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>
33 #include <account_ipc_marshal.h>
34 #include <account-private.h>
36 #include <account-error.h>
37 #include "account-server-private.h"
38 #include "account-server-db.h"
40 typedef sqlite3_stmt* account_stmt;
42 #define TEST_APP_ID "org.tizen.MyAccountCoreTest"
43 #define EAS_CMDLINE "/usr/bin/eas-engine"
44 #define EMAIL_SERVICE_CMDLINE "/usr/bin/email-service"
45 #define IMS_ENGINE_CMDLINE "/usr/bin/ims-srv"
46 #define IMS_AGENT_CMDLINE "/usr/bin/ims-agent"
47 #define MDM_SERVER_CMDLINE "/usr/bin/mdm-server"
49 #define RCS_APPID "com.samsung.rcs-im"
50 #define IMS_SERVICE_APPID "ims-service"
51 #define ACTIVESYNC_APPID "activesync-ui"
52 #define EMAIL_APPID "email-setting-efl"
53 #define SYNCHRONISE_APPID "setting-synchronise-efl"
54 #define DS_AGENT_CMDLINE "/usr/bin/oma-ds-agent"
56 #define FACEBOOK_SDK_APPID "com.samsung.facebook-service"
57 #define FACEBOOK_APPID "com.samsung.facebook"
59 #define ACCOUNT_DB_OPEN_READONLY 0
60 #define ACCOUNT_DB_OPEN_READWRITE 1
64 #define _TIZEN_PUBLIC_
65 #ifndef _TIZEN_PUBLIC_
66 //#include <csc-feature.h>
70 static sqlite3* g_hAccountDB = NULL;
71 static sqlite3* g_hAccountDB2 = NULL;
72 pthread_mutex_t account_mutex = PTHREAD_MUTEX_INITIALIZER;
74 static char *_account_get_text(const char *text_data);
75 static const char *_account_query_table_column_text(account_stmt pStmt, int pos);
76 static int _account_insert_custom(account_s *account, int account_id);
77 static int _account_update_custom(account_s *account, int account_id);
78 static int _account_query_custom_by_account_id(account_custom_cb callback, int account_id, void *user_data );
79 static int _account_type_update_provider_feature(account_type_s *account_type, const char* app_id);
81 int _account_query_capability_by_account_id(capability_cb callback, int account_id, void *user_data );
83 static void _account_insert_delete_update_notification_send(char *noti_name)
86 _ERR("Noti Name is NULL!!!!!!\n");
90 _INFO("noti_type = %s", noti_name);
92 if (vconf_set_str(VCONFKEY_ACCOUNT_MSG_STR, noti_name) != 0) {
93 _ERR("Vconf MSG Str set FAILED !!!!!!\n");;
97 int _account_get_current_appid_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
101 GSList** appid_list = (GSList**)user_data;
104 pkgmgr_ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
106 if( pkgmgr_ret != PMINFO_R_OK ){
107 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_appid(%d)", pkgmgr_ret);
110 item = _account_get_text(appid);
111 *appid_list = g_slist_append(*appid_list, item);
116 static inline int __read_proc(const char *path, char *buf, int size)
120 if (buf == NULL || path == NULL) {
121 ACCOUNT_ERROR("path and buffer is mandatory\n");
125 fd = open(path, O_RDONLY);
127 ACCOUNT_ERROR("fd open error(%d)\n", fd);
131 ret = read(fd, buf, size - 1);
133 ACCOUNT_ERROR("fd read error(%d)\n", fd);
144 char *_account_get_proc_cmdline_bypid(int pid)
149 ACCOUNT_SNPRINTF(buf, sizeof(buf), "/proc/%d/cmdline", pid);
150 ret = __read_proc(buf, buf, sizeof(buf));
152 ACCOUNT_DEBUG("No proc directory (%d)\n", pid);
160 static char* _account_get_current_appid(int pid)
162 _INFO("getting caller appid with pid=[%d]", pid);
165 char appid[128]={0,};
166 char* appid_ret = NULL;
168 ret = aul_app_get_appid_bypid(pid, appid, sizeof(appid));
171 ACCOUNT_ERROR("fail to get current appid ret=[%d], appid=%s\n", ret, appid);
176 /* SLP platform core exception */
177 if(strlen(appid) == 0){
179 char* cmdline = NULL;
180 cmdline = _account_get_proc_cmdline_bypid(pid);
181 ACCOUNT_SLOGD("cmdline (%s)!!!!!!\n", cmdline);
182 if(!g_strcmp0(cmdline, EAS_CMDLINE)) {
183 appid_ret = _account_get_text(ACTIVESYNC_APPID);
184 _ACCOUNT_FREE(cmdline);
186 } else if (!g_strcmp0(cmdline, EMAIL_SERVICE_CMDLINE) || !g_strcmp0(cmdline, MDM_SERVER_CMDLINE)) {
187 appid_ret = _account_get_text(EMAIL_APPID);
188 _ACCOUNT_FREE(cmdline);
190 } else if (!g_strcmp0(cmdline, IMS_ENGINE_CMDLINE) || !g_strcmp0(cmdline, IMS_AGENT_CMDLINE)) {
191 if(_account_type_query_app_id_exist(RCS_APPID)==ACCOUNT_ERROR_NONE){
192 appid_ret = _account_get_text(RCS_APPID);
193 } else if(_account_type_query_app_id_exist(IMS_SERVICE_APPID)==ACCOUNT_ERROR_NONE){
194 appid_ret = _account_get_text(IMS_SERVICE_APPID);
196 appid_ret = _account_get_text(RCS_APPID);
198 _ACCOUNT_FREE(cmdline);
200 } else if (!g_strcmp0(cmdline, DS_AGENT_CMDLINE)) {
201 appid_ret = _account_get_text(SYNCHRONISE_APPID);
202 _ACCOUNT_FREE(cmdline);
205 ACCOUNT_DEBUG("No app id\n");
206 _ACCOUNT_FREE(cmdline);
212 /* temporary exception */
213 if(!g_strcmp0(appid, "com.samsung.gallery")){
214 appid_ret = _account_get_text("com.samsung.facebook");
215 } else if(!g_strcmp0(appid, FACEBOOK_SDK_APPID)){
216 appid_ret = _account_get_text(FACEBOOK_APPID);
218 appid_ret = _account_get_text(appid);
224 static int _account_check_account_type_with_appid_group(const char* appid, char** verified_appid)
226 int error_code = ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
227 pkgmgrinfo_appinfo_h ahandle=NULL;
228 pkgmgrinfo_pkginfo_h phandle=NULL;
229 char* package_id = NULL;
230 GSList* appid_list = NULL;
234 ACCOUNT_ERROR("input param is null\n");
235 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
239 ACCOUNT_ERROR("output param is null\n");
240 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
243 if(!strcmp(appid, "com.samsung.setting")){
244 ACCOUNT_DEBUG("Setting exception\n");
245 *verified_appid = _account_get_text("com.samsung.setting");
246 return ACCOUNT_ERROR_NONE;
249 if(!strcmp(appid, "com.samsung.samsung-account-front")){
250 ACCOUNT_DEBUG("Setting exception\n");
251 *verified_appid = _account_get_text("com.samsung.samsung-account-front");
252 return ACCOUNT_ERROR_NONE;
255 if(!strcmp(appid, IMS_SERVICE_APPID) || !strcmp(appid, RCS_APPID)){
256 ACCOUNT_DEBUG("ims service exception\n");
257 *verified_appid = _account_get_text(appid);
258 return ACCOUNT_ERROR_NONE;
261 /* Get app id family which is stored in account database */
263 pkgmgr_ret = pkgmgrinfo_appinfo_get_appinfo(appid, &ahandle);
264 if( pkgmgr_ret != PMINFO_R_OK ){
265 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_appinfo(%d)", pkgmgr_ret);
267 pkgmgr_ret = pkgmgrinfo_appinfo_get_pkgid(ahandle, &package_id);
268 if( pkgmgr_ret != PMINFO_R_OK ){
269 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_pkgid(%d)", pkgmgr_ret);
271 pkgmgr_ret = pkgmgrinfo_pkginfo_get_pkginfo(package_id, &phandle);
272 if( pkgmgr_ret != PMINFO_R_OK ){
273 ACCOUNT_DEBUG("pkgmgrinfo_pkginfo_get_pkginfo(%d)", pkgmgr_ret);
275 pkgmgr_ret = pkgmgrinfo_appinfo_get_list(phandle, PMINFO_ALL_APP, _account_get_current_appid_cb, (void *)&appid_list);
276 if( pkgmgr_ret != PMINFO_R_OK ){
277 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_list(%d)", pkgmgr_ret);
280 /* Compare current app id with the stored app id family */
281 for(iter=appid_list;iter!=NULL;iter=g_slist_next(iter)){
282 char* tmp = (char*)iter->data;
284 if(_account_type_query_app_id_exist(tmp) == ACCOUNT_ERROR_NONE) {
285 *verified_appid = _account_get_text(tmp);
286 error_code = ACCOUNT_ERROR_NONE;
290 ACCOUNT_SLOGD("not matched owner group app id(%s), current appid(%s)\n", tmp, appid);
296 g_slist_free(appid_list);
297 pkgmgr_ret = pkgmgrinfo_pkginfo_destroy_pkginfo(phandle);
298 if( pkgmgr_ret != PMINFO_R_OK ){
299 ACCOUNT_DEBUG("pkgmgrinfo_pkginfo_destroy_pkginfo(%d)", pkgmgr_ret);
302 pkgmgr_ret = pkgmgrinfo_appinfo_destroy_appinfo(ahandle);
303 if( pkgmgr_ret != PMINFO_R_OK ){
304 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_destroy_appinfo(%d)", pkgmgr_ret);
310 static int _account_check_appid_group_with_package_name(const char* appid, char* package_name)
312 int error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
313 pkgmgrinfo_appinfo_h ahandle=NULL;
314 pkgmgrinfo_pkginfo_h phandle=NULL;
315 char* package_id = NULL;
316 GSList* appid_list = NULL;
320 ACCOUNT_ERROR("input param -appid is null\n");
321 return ACCOUNT_ERROR_INVALID_PARAMETER;
325 ACCOUNT_ERROR("input param - package name is null\n");
326 return ACCOUNT_ERROR_INVALID_PARAMETER;
329 /* ims-service Exception */
330 if ( strcmp(appid, "ims-service") == 0 && strcmp(package_name, "ims-service") == 0 ) {
331 ACCOUNT_DEBUG("ims exception."); // TODO: NEED TO REMOVE, debug log.
332 return ACCOUNT_ERROR_NONE;
335 /* Get app id family which is stored in account database */
337 pkgmgr_ret = pkgmgrinfo_appinfo_get_appinfo(appid, &ahandle);
338 if( pkgmgr_ret != PMINFO_R_OK ){
339 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_appinfo(%d)", pkgmgr_ret);
341 pkgmgr_ret = pkgmgrinfo_appinfo_get_pkgid(ahandle, &package_id);
342 if( pkgmgr_ret != PMINFO_R_OK ){
343 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_pkgid(%d)", pkgmgr_ret);
345 pkgmgr_ret = pkgmgrinfo_pkginfo_get_pkginfo(package_id, &phandle);
346 if( pkgmgr_ret != PMINFO_R_OK ){
347 ACCOUNT_DEBUG("pkgmgrinfo_pkginfo_get_pkginfo(%d)", pkgmgr_ret);
349 pkgmgr_ret = pkgmgrinfo_appinfo_get_list(phandle, PMINFO_ALL_APP, _account_get_current_appid_cb, (void *)&appid_list);
350 if( pkgmgr_ret != PMINFO_R_OK ){
351 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_list(%d)", pkgmgr_ret);
354 /* Compare current app id with the stored app id family */
355 for(iter=appid_list;iter!=NULL;iter=g_slist_next(iter)){
356 char* tmp = (char*)iter->data;
358 //ACCOUNT_ERROR("tmp(%s)package_name(%s)\n\n", tmp, package_name); // TODO: NEED TO REMOVE, debug log.
359 if( strcmp(tmp, package_name) == 0) {
360 error_code = ACCOUNT_ERROR_NONE;
363 } else if ( strcmp(tmp, "com.samsung.samsung-account-front") == 0 &&
364 strcmp(package_name, "gr47by21a5.SamsungAccount") == 0 ) {
365 /* Samung Account Exception */
366 error_code = ACCOUNT_ERROR_NONE;
370 ACCOUNT_SLOGD("not matched owner group app id(%s), current appid(%s)\n", tmp, appid);
376 g_slist_free(appid_list);
377 pkgmgr_ret = pkgmgrinfo_pkginfo_destroy_pkginfo(phandle);
378 if( pkgmgr_ret != PMINFO_R_OK ){
379 ACCOUNT_DEBUG("pkgmgrinfo_pkginfo_destroy_pkginfo(%d)", pkgmgr_ret);
382 pkgmgr_ret = pkgmgrinfo_appinfo_destroy_appinfo(ahandle);
383 if( pkgmgr_ret != PMINFO_R_OK ){
384 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_destroy_appinfo(%d)", pkgmgr_ret);
390 static int _remove_sensitive_info_from_non_owning_account(int caller_pid, account_s *account)
395 return ACCOUNT_ERROR_INVALID_PARAMETER;
398 if (account->package_name)
400 char *caller_package_name = _account_get_current_appid(caller_pid);
401 if (caller_package_name == NULL)
403 _ERR("Could not get caller app id, so removing sensitive info from account id [%d]", account->id);
404 return ACCOUNT_ERROR_INVALID_PARAMETER;
407 if (g_strcmp0(caller_package_name, account->package_name) != 0)
409 // packages dont match, so remove sensitive info
410 _INFO("Removing sensitive info from account id [%d]", account->id);
411 free (account->access_token);
412 account->access_token = NULL;
415 _ACCOUNT_FREE(caller_package_name);
416 return ACCOUNT_ERROR_NONE;
418 return ACCOUNT_ERROR_INVALID_PARAMETER;
421 static int _remove_sensitive_info_from_non_owning_account_list(int caller_pid, GList *account_list)
423 int return_code = ACCOUNT_ERROR_NONE;
425 if (account_list == NULL)
428 return ACCOUNT_ERROR_INVALID_PARAMETER;
431 GList *list_iter = NULL;
432 for (list_iter = account_list; list_iter != NULL; list_iter = g_list_next(list_iter))
434 account_s *account = (account_s *) list_iter->data;
435 int ret = _remove_sensitive_info_from_non_owning_account(caller_pid, account);
436 if( ret != ACCOUNT_ERROR_NONE)
442 static int _remove_sensitive_info_from_non_owning_account_slist(int caller_pid, GSList *account_list)
444 int return_code = ACCOUNT_ERROR_NONE;
446 if (account_list == NULL)
449 return ACCOUNT_ERROR_INVALID_PARAMETER;
452 GSList *list_iter = NULL;
453 for (list_iter = account_list; list_iter != NULL; list_iter = g_slist_next(list_iter))
455 account_s *account = (account_s *) list_iter->data;
456 int ret = _remove_sensitive_info_from_non_owning_account(caller_pid, account);
457 if( ret != ACCOUNT_ERROR_NONE)
463 static const char *_account_db_err_msg()
465 return sqlite3_errmsg(g_hAccountDB);
468 static int _account_db_err_code()
470 return sqlite3_errcode(g_hAccountDB);
473 static int _account_get_record_count(char* query)
475 _INFO("_account_get_record_count");
479 account_stmt pStmt = NULL;
482 _ERR("NULL query\n");
483 return ACCOUNT_ERROR_QUERY_SYNTAX_ERROR;
487 _ERR("DB is not opened\n");
488 return ACCOUNT_ERROR_DB_NOT_OPENED;
491 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
493 if (SQLITE_BUSY == rc){
494 _ERR("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg());
495 sqlite3_finalize(pStmt);
496 return ACCOUNT_ERROR_DATABASE_BUSY;
497 } else if (SQLITE_OK != rc) {
498 _ERR("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg());
499 sqlite3_finalize(pStmt);
500 return ACCOUNT_ERROR_DB_FAILED;
503 rc = sqlite3_step(pStmt);
504 if (SQLITE_BUSY == rc) {
505 _ERR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg());
506 sqlite3_finalize(pStmt);
507 return ACCOUNT_ERROR_DATABASE_BUSY;
508 } else if (SQLITE_ROW != rc) {
509 _ERR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg());
510 sqlite3_finalize(pStmt);
511 return ACCOUNT_ERROR_DB_FAILED;
514 ncount = sqlite3_column_int(pStmt, 0);
516 _INFO("account record count [%d]", ncount);
517 sqlite3_finalize(pStmt);
522 static int _account_execute_query(const char *query)
525 char* pszErrorMsg = NULL;
528 ACCOUNT_ERROR("NULL query\n");
529 return ACCOUNT_ERROR_QUERY_SYNTAX_ERROR;
533 ACCOUNT_ERROR("DB is not opened\n");
534 return ACCOUNT_ERROR_DB_NOT_OPENED;
537 rc = sqlite3_exec(g_hAccountDB, query, NULL, NULL, &pszErrorMsg);
538 if (SQLITE_OK != rc) {
539 ACCOUNT_ERROR("sqlite3_exec rc(%d) query(%s) failed(%s).", rc, query, pszErrorMsg);
540 sqlite3_free(pszErrorMsg);
546 static int _account_begin_transaction(void)
548 ACCOUNT_DEBUG("_account_begin_transaction start");
551 ret = _account_execute_query("BEGIN IMMEDIATE TRANSACTION");
553 if (ret == SQLITE_BUSY){
554 ACCOUNT_ERROR(" sqlite3 busy = %d", ret);
555 return ACCOUNT_ERROR_DATABASE_BUSY;
556 } else if(ret != SQLITE_OK) {
557 ACCOUNT_ERROR("_account_svc_begin_transaction fail :: %d", ret);
558 return ACCOUNT_ERROR_DB_FAILED;
561 ACCOUNT_DEBUG("_account_begin_transaction end");
562 return ACCOUNT_ERROR_NONE;
565 static int _account_end_transaction(bool is_success)
567 ACCOUNT_DEBUG("_account_end_transaction start");
571 if (is_success == true) {
572 ret = _account_execute_query("COMMIT TRANSACTION");
573 ACCOUNT_DEBUG("_account_end_transaction COMMIT");
575 ret = _account_execute_query("ROLLBACK TRANSACTION");
576 ACCOUNT_DEBUG("_account_end_transaction ROLLBACK");
579 if(ret == SQLITE_PERM){
580 ACCOUNT_ERROR("Account permission denied :: %d", ret);
581 return ACCOUNT_ERROR_PERMISSION_DENIED;
584 if (ret == SQLITE_BUSY){
585 ACCOUNT_DEBUG(" sqlite3 busy = %d", ret);
586 return ACCOUNT_ERROR_DATABASE_BUSY;
589 if (ret != SQLITE_OK) {
590 ACCOUNT_ERROR("_account_svc_end_transaction fail :: %d", ret);
591 return ACCOUNT_ERROR_DB_FAILED;
594 ACCOUNT_DEBUG("_account_end_transaction end");
595 return ACCOUNT_ERROR_NONE;
598 static bool _account_check_add_more_account(const char* app_id)
600 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
603 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
604 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
606 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
608 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s' and MultipleAccountSupport = 1", ACCOUNT_TYPE_TABLE, app_id);
609 rc = _account_get_record_count(query);
611 /* multiple account support case */
613 ACCOUNT_SLOGD("app id (%s) supports multiple account. rc(%d)\n", app_id, rc);
617 /* multiple account not support case */
618 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
619 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE package_name = '%s'", ACCOUNT_TABLE, app_id);
620 rc = _account_get_record_count(query);
623 ACCOUNT_SLOGD("app id (%s) supports single account. and there is no account of the app id\n", app_id);
630 //TODO: Need to enable creating db on the first connect for
631 //a) multi-user cases
632 //b) to ensure db exist in every connect call
634 //static int _account_create_all_tables(void)
637 // int error_code = ACCOUNT_ERROR_NONE;
638 // char query[ACCOUNT_SQL_LEN_MAX] = {0, };
640 // ACCOUNT_DEBUG("create all table - BEGIN");
641 // ACCOUNT_MEMSET(query, 0, sizeof(query));
643 // /*Create the account table*/
644 // ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_TABLE);
645 // rc = _account_get_record_count(query);
647 // rc = _account_execute_query(ACCOUNT_SCHEMA);
648 // if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY;
649 // 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()));
651 //#ifndef _TIZEN_PUBLIC_
652 // if (CSC_FEATURE_BOOL_TRUE == csc_feature_get_bool(CSC_FEATURE_DEF_BOOL_CONTACTS_DOCOMO_SOCIAL_PHONEBOOK)) {
653 // /* NTT docomo specific area */
654 // rc = _account_execute_query(DOCOMO_DEFAULT_VAL_INSERT_QUERY);
655 // if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY;
656 // 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()));
657 // /* END of NTT docomo specific area */
662 // /*Create capability table*/
663 // ACCOUNT_MEMSET(query, 0, sizeof(query));
664 // ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", CAPABILITY_TABLE);
665 // rc = _account_get_record_count(query);
667 // rc = _account_execute_query(CAPABILITY_SCHEMA);
668 // if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY;
669 // 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()));
672 // /* Create account custom table */
673 // ACCOUNT_MEMSET(query, 0, sizeof(query));
674 // ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_CUSTOM_TABLE);
675 // rc = _account_get_record_count(query);
677 // rc = _account_execute_query(ACCOUNT_CUSTOM_SCHEMA);
678 // if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY;
679 // ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
682 // /* Create account type table */
683 // ACCOUNT_MEMSET(query, 0, sizeof(query));
684 // ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_TYPE_TABLE);
685 // rc = _account_get_record_count(query);
687 // rc = _account_execute_query(ACCOUNT_TYPE_SCHEMA);
688 // if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY;
689 // 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()));
692 // /* Create label table */
693 // ACCOUNT_MEMSET(query, 0, sizeof(query));
694 // ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", LABEL_TABLE);
695 // rc = _account_get_record_count(query);
697 // rc = _account_execute_query(LABEL_SCHEMA);
698 // if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY;
699 // 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()));
702 // /* Create account feature table */
703 // ACCOUNT_MEMSET(query, 0, sizeof(query));
704 // ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", PROVIDER_FEATURE_TABLE);
705 // rc = _account_get_record_count(query);
707 // rc = _account_execute_query(PROVIDER_FEATURE_SCHEMA);
708 // if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY;
709 // 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()));
712 // ACCOUNT_DEBUG("create all table - END");
713 // return error_code;
716 //static int _account_check_is_all_table_exists()
719 // char query[ACCOUNT_SQL_LEN_MAX] = {0,};
720 // ACCOUNT_MEMSET(query, 0, sizeof(query));
722 // ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s', '%s', '%s', '%s', '%s', '%s')",
723 // ACCOUNT_TABLE, CAPABILITY_TABLE, ACCOUNT_CUSTOM_TABLE, ACCOUNT_TYPE_TABLE, LABEL_TABLE, PROVIDER_FEATURE_TABLE);
724 // rc = _account_get_record_count(query);
726 // if (rc != ACCOUNT_TABLE_TOTAL_COUNT) {
727 // ACCOUNT_ERROR("Table count is not matched rc=%d\n", rc);
733 int _account_db_handle_close(sqlite3* hDB)
736 int ret = ACCOUNT_ERROR_NONE;
739 rc = db_util_close(hDB);
740 if( rc == SQLITE_OK )
741 ret = ACCOUNT_ERROR_NONE;
742 else if( rc == SQLITE_PERM )
743 ret = ACCOUNT_ERROR_PERMISSION_DENIED;
744 else if ( rc == SQLITE_BUSY )
745 ret = ACCOUNT_ERROR_DATABASE_BUSY;
747 ret = ACCOUNT_ERROR_DB_FAILED;
752 int _account_db_open(int mode, int pid)
756 char account_db_path[256] = {0, };
758 _INFO( "start _account_db_open()");
760 ACCOUNT_MEMSET(account_db_path, 0x00, sizeof(account_db_path));
761 ACCOUNT_SNPRINTF(account_db_path, sizeof(account_db_path), "%s", ACCOUNT_DB_PATH);
764 _ERR( "Account database is using in another app. %x", g_hAccountDB );
765 return ACCOUNT_ERROR_DATABASE_BUSY;
768 ret = _account_db_handle_close(g_hAccountDB2);
769 if( ret != ACCOUNT_ERROR_NONE )
770 ACCOUNT_DEBUG( "db_util_close(g_hAccountDB2) fail ret = %d", ret);
772 ACCOUNT_DEBUG( "before db_util_open()");
773 if(mode == ACCOUNT_DB_OPEN_READWRITE)
774 rc = db_util_open(account_db_path, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD);
775 else if(mode == ACCOUNT_DB_OPEN_READONLY)
776 rc = db_util_open_with_options(account_db_path, &g_hAccountDB, SQLITE_OPEN_READONLY, NULL);
778 return ACCOUNT_ERROR_DB_NOT_OPENED;
779 ACCOUNT_DEBUG( "after db_util_open() sqlite_rc = %d", rc);
781 if( rc == SQLITE_PERM || _account_db_err_code() == SQLITE_PERM ) {
782 ACCOUNT_ERROR( "Account permission denied");
783 return ACCOUNT_ERROR_PERMISSION_DENIED;
786 if( rc == SQLITE_BUSY ) {
787 ACCOUNT_ERROR( "busy handler fail.");
788 return ACCOUNT_ERROR_DATABASE_BUSY;
791 if( rc != SQLITE_OK ) {
792 ACCOUNT_ERROR( "The database isn't connected." );
793 return ACCOUNT_ERROR_DB_NOT_OPENED;
796 _INFO( "end _account_db_open()");
797 return ACCOUNT_ERROR_NONE;
800 int _account_db_close(void)
802 ACCOUNT_DEBUG( "start db_util_close()");
805 ret = _account_db_handle_close(g_hAccountDB2);
806 if( ret != ACCOUNT_ERROR_NONE )
807 ACCOUNT_DEBUG( "db_util_close(g_hAccountDB2) fail ret = %d", ret);
809 ret = _account_db_handle_close(g_hAccountDB);
810 if( ret != ACCOUNT_ERROR_NONE )
812 ACCOUNT_ERROR( "db_util_close(g_hAccountDB) fail ret = %d", ret);
813 g_hAccountDB2 = g_hAccountDB;
820 static int _account_check_duplicated(account_s *data, const char* verified_appid)
822 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
826 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
828 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')"
829 , ACCOUNT_TABLE, verified_appid, data->user_name, data->display_name, data->email_address);
831 count = _account_get_record_count(query);
834 return ACCOUNT_ERROR_NONE;
837 //check whether duplicated account or not.
839 //2. check display_name
840 //3. check email_address
841 GList* account_list_temp = _account_query_account_by_package_name(getpid(), verified_appid, &ret);
842 if (account_list_temp == NULL)
844 _ERR("_account_query_account_by_package_name returned NULL");
845 return ACCOUNT_ERROR_DB_FAILED;
848 if( _account_db_err_code() == SQLITE_PERM ){
849 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
850 return ACCOUNT_ERROR_PERMISSION_DENIED;
853 if(ret != ACCOUNT_ERROR_NONE){
857 account_list_temp = g_list_first(account_list_temp);
858 _INFO("account_list_temp length=[%d]",g_list_length(account_list_temp));
861 for (iter = account_list_temp; iter != NULL; iter = g_list_next(iter))
863 _INFO("iterating account_list_temp");
864 account_s *account = NULL;
865 _INFO("Before iter->data");
866 account = (account_s*)iter->data;
867 _INFO("After iter->data");
870 if(account->user_name!=NULL && data->user_name!=NULL && strcmp(account->user_name, data->user_name)==0)
872 _INFO("duplicated account(s) exist!, same user_name=%s", data->user_name);
873 return ACCOUNT_ERROR_DUPLICATED;
875 //when user_name is not NULL and display_name is same.
876 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)
878 _INFO("duplicated account(s) exist!, same display_name=%s", data->display_name);
879 return ACCOUNT_ERROR_DUPLICATED;
881 //when user_name and display_name are not NULL and email_address is same.
882 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)
884 _INFO("duplicated account(s) exist!, same email_address=%s", data->email_address);
885 return ACCOUNT_ERROR_DUPLICATED;
890 return ACCOUNT_ERROR_NONE;
893 static int _account_get_next_sequence(const char *pszName)
896 account_stmt pStmt = NULL;
898 char szQuery[ACCOUNT_SQL_LEN_MAX] = {0,};
900 ACCOUNT_MEMSET(szQuery, 0x00, sizeof(szQuery));
901 ACCOUNT_SNPRINTF(szQuery, sizeof(szQuery), "SELECT max(seq) FROM %s where name = '%s' ", ACCOUNT_SQLITE_SEQ, pszName);
902 rc = sqlite3_prepare_v2(g_hAccountDB, szQuery, strlen(szQuery), &pStmt, NULL);
903 if (SQLITE_OK != rc) {
904 ACCOUNT_SLOGE("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg());
905 sqlite3_finalize(pStmt);
906 return ACCOUNT_ERROR_DB_FAILED;
909 rc = sqlite3_step(pStmt);
910 max_seq = sqlite3_column_int(pStmt, 0);
913 /*Finalize Statement*/
914 rc = sqlite3_finalize(pStmt);
920 static account_stmt _account_prepare_query(char *query)
923 account_stmt pStmt = NULL;
925 ACCOUNT_RETURN_VAL((query != NULL), {}, NULL, ("query is NULL"));
927 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
929 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, NULL, ("sqlite3_prepare_v2(%s) failed(%s).", query, _account_db_err_msg()));
934 static int _account_query_bind_int(account_stmt pStmt, int pos, int num)
937 ACCOUNT_ERROR("statement is null");
942 ACCOUNT_ERROR("invalid pos");
946 return sqlite3_bind_int(pStmt, pos, num);
949 static int _account_query_bind_text(account_stmt pStmt, int pos, const char *str)
951 _INFO("_account_query_bind_text");
955 _ERR("statement is null");
961 _INFO("sqlite3_bind_text");
962 return sqlite3_bind_text(pStmt, pos, (const char*)str, strlen(str), SQLITE_STATIC);
966 _INFO("sqlite3_bind_null");
967 return sqlite3_bind_null(pStmt, pos);
971 static int _account_convert_account_to_sql(account_s *account, account_stmt hstmt, char *sql_value)
977 /*Caution : Keep insert query orders.*/
980 _account_query_bind_text(hstmt, count++, (char*)account->user_name);
981 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], user_name=%s", account->id, account->user_name);
983 /* 2. email address*/
984 _account_query_bind_text(hstmt, count++, (char*)account->email_address);
985 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], email_address=%s", account->id, account->email_address);
988 _account_query_bind_text(hstmt, count++, (char*)account->display_name);
989 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], display_name=%s", account->id, account->display_name);
992 _account_query_bind_text(hstmt, count++, (char*)account->icon_path);
993 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], icon_path=%s", account->id, account->icon_path);
996 _account_query_bind_text(hstmt, count++, (char*)account->source);
997 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], source=%s", account->id, account->source);
1000 _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1001 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], package_name=%s", account->id, account->package_name);
1003 /* 7. access token*/
1004 _account_query_bind_text(hstmt, count++, (char*)account->access_token);
1005 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], access_token=%s", account->id, account->access_token);
1008 _account_query_bind_text(hstmt, count++, (char*)account->domain_name);
1009 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], domain_name=%s", account->id, account->domain_name);
1012 _account_query_bind_int(hstmt, count++, account->auth_type);
1013 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], auth_type=%d", account->id, account->auth_type);
1016 _account_query_bind_int(hstmt, count++, account->secret);
1017 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], secret=%d", account->id, account->secret);
1019 /* 11. sync_support */
1020 _account_query_bind_int(hstmt, count++, account->sync_support);
1021 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], sync_support=%d", account->id, account->sync_support);
1026 for(i=0; i< USER_TXT_CNT; i++)
1027 _account_query_bind_text(hstmt, count++, (char*)account->user_data_txt[i]);
1029 /* 13. user integer */
1030 for(i=0; i< USER_INT_CNT; i++)
1032 _account_query_bind_int(hstmt, count++, account->user_data_int[i]);
1033 _INFO("convert user_data_int : marshal_user_int data_int[%d]=%d", i, account->user_data_int[i]);
1041 static int _account_query_finalize(account_stmt pStmt)
1046 ACCOUNT_ERROR( "pStmt is NULL");
1047 return ACCOUNT_ERROR_INVALID_PARAMETER;
1050 rc = sqlite3_finalize(pStmt);
1051 if (rc == SQLITE_BUSY){
1052 ACCOUNT_ERROR(" sqlite3 busy = %d", rc);
1053 return ACCOUNT_ERROR_DATABASE_BUSY;
1054 } else if (rc != SQLITE_OK) {
1055 ACCOUNT_ERROR( "sqlite3_finalize fail, rc : %d, db_error : %s\n", rc, _account_db_err_msg());
1056 return ACCOUNT_ERROR_DB_FAILED;
1059 return ACCOUNT_ERROR_NONE;
1062 static int _account_query_step(account_stmt pStmt)
1065 ACCOUNT_ERROR( "pStmt is NULL");
1069 return sqlite3_step(pStmt);
1072 static int _account_execute_insert_query(account_s *account)
1074 _INFO("_account_execute_insert_query start");
1077 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1078 int error_code = ACCOUNT_ERROR_NONE;
1079 account_stmt hstmt = NULL;
1081 /* check whether app id exist in account type db */
1083 if (!account->user_name && !account->display_name && !account->email_address) {
1085 ACCOUNT_ERROR("Mandetory fields is NULL. At least one field is required among username, display name, email address\n");
1086 return ACCOUNT_ERROR_INVALID_PARAMETER;
1090 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1091 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (user_name, email_address , display_name , icon_path , source , package_name , "
1092 "access_token , domain_name , auth_type , secret , sync_support , txt_custom0, txt_custom1, txt_custom2, txt_custom3, txt_custom4, "
1093 "int_custom0, int_custom1, int_custom2, int_custom3, int_custom4, txt_custom0 ) values " // to do urusa
1094 "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", ACCOUNT_TABLE);
1096 hstmt = _account_prepare_query(query);
1097 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
1100 _account_convert_account_to_sql(account, hstmt, query);
1103 rc = _account_query_step(hstmt);
1104 if (rc != SQLITE_DONE) {
1106 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1108 if( _account_db_err_code() == SQLITE_PERM )
1109 error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
1111 error_code = ACCOUNT_ERROR_DB_FAILED;
1115 rc = _account_query_finalize(hstmt);
1116 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1119 _INFO("_account_execute_insert_query end");
1123 static int _account_insert_capability(account_s *account, int account_id)
1125 _INFO("_account_insert_capability start");
1127 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1128 account_stmt hstmt = NULL;
1130 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1132 if (g_slist_length( account->capablity_list)==0) {
1133 ACCOUNT_DEBUG( "_account_insert_capability, no capability\n");
1134 return ACCOUNT_ERROR_NONE;
1137 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
1139 _INFO("_account_insert_capability _account_get_record_count [%s]", query);
1140 rc = _account_get_record_count(query);
1142 if( _account_db_err_code() == SQLITE_PERM ){
1143 _ERR( "Access failed(%s)", _account_db_err_msg());
1144 return ACCOUNT_ERROR_PERMISSION_DENIED;
1147 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1154 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1158 account_capability_s* cap_data = NULL;
1159 cap_data = (account_capability_s*)iter->data;
1161 _INFO("cap_data->type = %s, cap_data->value = %d \n", cap_data->type, cap_data->value);
1163 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1164 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
1165 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
1166 hstmt = _account_prepare_query(query);
1168 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
1170 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
1171 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1172 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
1173 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1174 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1175 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1176 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1177 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1178 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
1179 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1181 rc = _account_query_step(hstmt);
1182 _INFO("_account_insert_capability _account_query_step[%d]", rc);
1184 if (rc != SQLITE_DONE) {
1185 _ERR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1189 rc = _account_query_finalize(hstmt);
1190 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1195 _INFO("_account_insert_capability end");
1196 return ACCOUNT_ERROR_NONE;
1199 static int _account_update_capability(account_s *account, int account_id)
1202 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1203 account_stmt hstmt = NULL;
1205 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1207 if (g_slist_length( account->capablity_list)==0) {
1208 ACCOUNT_ERROR( "_account_update_capability, no capability\n");
1209 return ACCOUNT_ERROR_NONE;
1212 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
1214 rc = _account_get_record_count(query);
1217 ACCOUNT_SLOGI( "_account_update_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
1218 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1221 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1223 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id=? ", CAPABILITY_TABLE);
1224 hstmt = _account_prepare_query(query);
1226 _account_query_bind_int(hstmt, count++, (int)account_id);
1227 rc = _account_query_step(hstmt);
1229 if (rc != SQLITE_DONE) {
1230 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1231 return ACCOUNT_ERROR_DB_FAILED;
1233 rc = _account_query_finalize(hstmt);
1234 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1239 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1242 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1243 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
1244 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
1246 hstmt = _account_prepare_query(query);
1248 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
1250 account_capability_s* cap_data = NULL;
1251 cap_data = (account_capability_s*)iter->data;
1253 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
1254 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1255 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
1256 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1257 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1258 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1259 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1260 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1261 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
1262 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1264 rc = _account_query_step(hstmt);
1266 if (rc != SQLITE_DONE) {
1267 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1271 rc = _account_query_finalize(hstmt);
1272 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1277 return ACCOUNT_ERROR_NONE;
1280 static int _account_update_capability_by_user_name(account_s *account, const char *user_name, const char *package_name )
1283 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1284 account_stmt hstmt = NULL;
1286 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1288 if (g_slist_length( account->capablity_list)==0) {
1289 ACCOUNT_ERROR( "_account_update_capability_by_user_name, no capability\n");
1290 return ACCOUNT_ERROR_NONE;
1293 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where package_name= '%s' and user_name='%s'", ACCOUNT_TABLE, package_name, user_name);
1295 rc = _account_get_record_count(query);
1298 ACCOUNT_SLOGI( "_account_update_capability_by_user_name : related account item is not existed rc=%d , %s ", rc, _account_db_err_msg());
1299 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1302 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1304 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name=? and user_name=? ", CAPABILITY_TABLE);
1305 hstmt = _account_prepare_query(query);
1307 _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1308 _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1309 rc = _account_query_step(hstmt);
1310 if (rc != SQLITE_DONE) {
1311 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1312 return ACCOUNT_ERROR_DB_FAILED;
1315 rc = _account_query_finalize(hstmt);
1316 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1321 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1324 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1325 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
1326 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
1328 hstmt = _account_prepare_query(query);
1330 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
1332 account_capability_s* cap_data = NULL;
1333 cap_data = (account_capability_s*)iter->data;
1335 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
1336 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1337 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
1338 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1339 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1340 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1341 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1342 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1343 ret = _account_query_bind_int(hstmt, count++, (int)account->id);
1344 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1346 rc = _account_query_step(hstmt);
1348 if (rc != SQLITE_DONE) {
1349 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1353 rc = _account_query_finalize(hstmt);
1354 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1359 return ACCOUNT_ERROR_NONE;
1362 static int _account_query_table_column_int(account_stmt pStmt, int pos)
1365 ACCOUNT_ERROR("statement is null");
1370 ACCOUNT_ERROR("invalid pos");
1374 return sqlite3_column_int(pStmt, pos);
1377 static const char *_account_query_table_column_text(account_stmt pStmt, int pos)
1380 ACCOUNT_ERROR("statement is null");
1385 ACCOUNT_ERROR("invalid pos");
1389 return (const char*)sqlite3_column_text(pStmt, pos);
1392 static void _account_db_data_to_text(const char *textbuf, char **output)
1394 if (textbuf && strlen(textbuf)>0) {
1399 *output = strdup(textbuf);
1403 static void _account_convert_column_to_account(account_stmt hstmt, account_s *account_record)
1405 const char *textbuf = NULL;
1407 account_record->id = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_ID);
1408 ACCOUNT_DEBUG("account_record->id =[%d]", account_record->id);
1410 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_NAME);
1411 _account_db_data_to_text(textbuf, &(account_record->user_name));
1413 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_EMAIL_ADDRESS);
1414 _account_db_data_to_text(textbuf, &(account_record->email_address));
1416 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DISPLAY_NAME);
1417 _account_db_data_to_text(textbuf, &(account_record->display_name));
1419 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ICON_PATH);
1420 _account_db_data_to_text(textbuf, &(account_record->icon_path));
1422 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_SOURCE);
1423 _account_db_data_to_text(textbuf, &(account_record->source));
1425 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_PACKAGE_NAME);
1426 _account_db_data_to_text(textbuf, &(account_record->package_name));
1428 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ACCESS_TOKEN);
1429 _account_db_data_to_text(textbuf, &(account_record->access_token));
1431 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DOMAIN_NAME);
1432 _account_db_data_to_text(textbuf, &(account_record->domain_name));
1434 account_record->auth_type = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_AUTH_TYPE);
1436 account_record->secret = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SECRET);
1438 account_record->sync_support = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SYNC_SUPPORT);
1440 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_0);
1441 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[0]));
1443 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_1);
1444 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[1]));
1446 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_2);
1447 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[2]));
1449 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_3);
1450 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[3]));
1452 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_4);
1453 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[4]));
1455 account_record->user_data_int[0] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_0);
1456 account_record->user_data_int[1] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_1);
1457 account_record->user_data_int[2] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_2);
1458 account_record->user_data_int[3] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_3);
1459 account_record->user_data_int[4] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_4);
1462 static void _account_convert_column_to_capability(account_stmt hstmt, account_capability_s *capability_record)
1464 const char *textbuf = NULL;
1466 _INFO("start _account_convert_column_to_capability()");
1467 capability_record->id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ID);
1469 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_KEY);
1470 _account_db_data_to_text(textbuf, &(capability_record->type));
1472 capability_record->value = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_VALUE);
1474 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_PACKAGE_NAME);
1475 _account_db_data_to_text(textbuf, &(capability_record->package_name));
1477 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_USER_NAME);
1478 _account_db_data_to_text(textbuf, &(capability_record->user_name));
1480 capability_record->account_id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ACCOUNT_ID);
1481 _INFO("type = %s, value = %d", capability_record->type, capability_record->value);
1482 _INFO("end _account_convert_column_to_capability()");
1485 static void _account_convert_column_to_custom(account_stmt hstmt, account_custom_s *custom_record)
1487 _INFO("start _account_convert_column_to_custom()");
1488 const char *textbuf = NULL;
1490 custom_record->account_id = _account_query_table_column_int(hstmt, ACCOUNT_CUSTOM_FIELD_ACCOUNT_ID);
1492 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_APP_ID);
1493 _account_db_data_to_text(textbuf, &(custom_record->app_id));
1495 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_KEY);
1496 _account_db_data_to_text(textbuf, &(custom_record->key));
1498 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_VALUE);
1499 _account_db_data_to_text(textbuf, &(custom_record->value));
1500 _INFO("key = %s, value = %s", custom_record->key, custom_record->value);
1501 _INFO("end _account_convert_column_to_custom()");
1504 bool _account_get_capability_text_cb(const char* capability_type, account_capability_state_e capability_value, void *user_data)
1506 account_s *data = (account_s*)user_data;
1508 account_capability_s *cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
1510 if (cap_data == NULL)
1512 ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
1514 cap_data->type = _account_get_text(capability_type);
1515 cap_data->value = capability_value;
1516 _INFO("cap_data->type = %s, cap_data->value = %d", cap_data->type, cap_data->value);
1518 data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data);
1524 bool _account_get_custom_text_cb(char* key, char* value, void *user_data)
1526 account_s *data = (account_s*)user_data;
1528 account_custom_s *custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
1530 if (custom_data == NULL) {
1531 ACCOUNT_DEBUG("_account_get_custom_text_cb :: malloc fail\n");
1534 ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
1536 custom_data->account_id = data->id;
1537 custom_data->app_id = _account_get_text(data->package_name);
1538 custom_data->key = _account_get_text(key);
1539 custom_data->value = _account_get_text(value);
1540 _INFO("custom_data->key = %s, custom_data->value = %s", custom_data->key, custom_data->value);
1542 data->custom_list = g_slist_append(data->custom_list, (gpointer)custom_data);
1548 static char *_account_get_text(const char *text_data)
1550 char *text_value = NULL;
1552 if (text_data != NULL) {
1553 text_value = strdup(text_data);
1558 static int _account_compare_old_record_by_user_name(account_s *new_account, const char* user_name, const char* package_name)
1560 int error_code = ACCOUNT_ERROR_NONE;
1561 account_stmt hstmt = NULL;
1562 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1564 account_s *old_account = NULL;
1566 ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1567 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
1568 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
1569 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1571 old_account = (account_s*)calloc(1, sizeof(account_s));
1573 ACCOUNT_FATAL("Memory alloc fail\n");
1574 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1577 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1579 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = '%s' and package_name='%s'", ACCOUNT_TABLE, user_name, package_name);
1580 hstmt = _account_prepare_query(query);
1582 rc = _account_query_step(hstmt);
1583 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1585 while (rc == SQLITE_ROW) {
1586 _account_convert_column_to_account(hstmt, old_account);
1587 rc = _account_query_step(hstmt);
1590 rc = _account_query_finalize(hstmt);
1591 ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1595 error_code = _account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account);
1596 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
1599 error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account);
1600 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
1603 new_account->id = old_account->id;
1606 if(!new_account->user_name) {
1607 if(old_account->user_name)
1608 new_account->user_name = _account_get_text(old_account->user_name);
1612 if(!new_account->display_name) {
1613 if(old_account->display_name)
1614 new_account->display_name = _account_get_text(old_account->display_name);
1618 if(!new_account->email_address) {
1619 if(old_account->email_address)
1620 new_account->email_address = _account_get_text(old_account->email_address);
1624 if(!new_account->domain_name) {
1625 if(old_account->domain_name)
1626 new_account->domain_name = _account_get_text(old_account->domain_name);
1630 if(!new_account->icon_path) {
1631 if(old_account->icon_path)
1632 new_account->icon_path = _account_get_text(old_account->icon_path);
1636 if(!new_account->source) {
1637 if(old_account->source)
1638 new_account->source = _account_get_text(old_account->source);
1641 _ACCOUNT_FREE(new_account->package_name);
1642 new_account->package_name = _account_get_text(old_account->package_name);
1645 if(!new_account->access_token) {
1646 if(old_account->access_token)
1647 new_account->access_token = _account_get_text(old_account->access_token);
1651 if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) {
1652 new_account->auth_type = old_account->auth_type;
1656 if(new_account->secret== ACCOUNT_SECRECY_INVALID) {
1657 new_account->secret = old_account->secret;
1661 if(new_account->sync_support == ACCOUNT_SYNC_INVALID) {
1662 new_account->sync_support = old_account->sync_support;
1667 for(i=0;i<USER_TXT_CNT;i++) {
1668 if(!new_account->user_data_txt[i]) {
1669 if(old_account->user_data_txt[i])
1670 new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]);
1675 for(i=0;i<USER_INT_CNT;i++) {
1676 if(new_account->user_data_int[i] == 0) {
1677 new_account->user_data_int[i] = old_account->user_data_int[i];
1683 // user custom table
1687 _account_free_account_items(old_account);
1688 _ACCOUNT_FREE(old_account);
1691 if (hstmt != NULL) {
1692 rc = _account_query_finalize(hstmt);
1693 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1697 return ACCOUNT_ERROR_NONE;
1702 static int _account_update_account_by_user_name(int pid, account_s *account, const char *user_name, const char *package_name)
1704 int rc = 0, binding_count = 0, count = 0;
1705 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1706 int error_code = ACCOUNT_ERROR_NONE;
1707 account_stmt hstmt = NULL;
1709 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is NULL.\n"));
1710 ACCOUNT_RETURN_VAL((package_name!= NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is NULL.\n"));
1712 char* current_appid = NULL;
1713 char* verified_appid = NULL;
1715 current_appid = _account_get_current_appid(pid);
1716 error_code = _account_check_account_type_with_appid_group(current_appid, &verified_appid);
1718 _ACCOUNT_FREE(current_appid);
1719 _ACCOUNT_FREE(verified_appid);
1721 if(error_code != ACCOUNT_ERROR_NONE){
1722 ACCOUNT_ERROR("No permission to update\n");
1723 return ACCOUNT_ERROR_PERMISSION_DENIED;
1726 _account_compare_old_record_by_user_name(account, user_name, package_name);
1728 if( _account_db_err_code() == SQLITE_PERM ){
1729 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1730 return ACCOUNT_ERROR_PERMISSION_DENIED;
1733 if (!account->package_name) {
1734 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
1735 return ACCOUNT_ERROR_INVALID_PARAMETER;
1738 if (!account->user_name && !account->display_name && !account->email_address) {
1739 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1740 return ACCOUNT_ERROR_INVALID_PARAMETER;
1743 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE user_name='%s' and package_name='%s'"
1744 , ACCOUNT_TABLE, user_name, package_name);
1746 count = _account_get_record_count(query);
1748 if( _account_db_err_code() == SQLITE_PERM ){
1749 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1750 return ACCOUNT_ERROR_PERMISSION_DENIED;
1754 ACCOUNT_SLOGI("_account_update_account_by_user_name : The account not exist!, count = %d, user_name=%s, package_name=%s\n",
1755 count, user_name, package_name);
1756 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1759 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1761 //TODO: Is it required to update id ? As of now I can only think of falied rollback cases (between account and gSSO DB)
1762 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1763 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
1764 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1765 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE user_name=? and package_name=? ", ACCOUNT_TABLE);
1767 hstmt = _account_prepare_query(query);
1768 if( _account_db_err_code() == SQLITE_PERM ){
1769 _account_end_transaction(FALSE);
1770 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1771 return ACCOUNT_ERROR_PERMISSION_DENIED;
1773 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
1775 binding_count = _account_convert_account_to_sql(account, hstmt, query);
1777 _account_query_bind_text(hstmt, binding_count++, user_name);
1778 _account_query_bind_text(hstmt, binding_count++, package_name);
1779 rc = _account_query_step(hstmt);
1780 if (rc != SQLITE_DONE) {
1781 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1783 rc = _account_query_finalize(hstmt);
1784 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1787 /*update capability*/
1788 error_code = _account_update_capability_by_user_name(account, user_name, package_name);
1791 error_code = _account_update_custom(account, account->id);
1796 int _account_insert_to_db(account_s* account, int pid, int *account_id)
1799 int error_code = ACCOUNT_ERROR_NONE;
1800 int ret_transaction = 0;
1802 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1803 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1804 ACCOUNT_RETURN_VAL((account_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID POINTER IS NULL"));
1806 if (!account->user_name && !account->display_name && !account->email_address) {
1807 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1808 return ACCOUNT_ERROR_INVALID_PARAMETER;
1811 account_s *data = (account_s*)account;
1812 ACCOUNT_SLOGD("(%s)-(%d) account_insert_to_db: begin_transaction.\n", __FUNCTION__, __LINE__);
1814 pthread_mutex_lock(&account_mutex);
1816 /* transaction control required*/
1817 ret_transaction = _account_begin_transaction();
1819 if(_account_db_err_code() == SQLITE_PERM){
1820 pthread_mutex_unlock(&account_mutex);
1821 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1822 return ACCOUNT_ERROR_PERMISSION_DENIED;
1825 if (ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY) {
1826 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
1827 pthread_mutex_unlock(&account_mutex);
1828 return ACCOUNT_ERROR_DATABASE_BUSY;
1829 }else if (ret_transaction != ACCOUNT_ERROR_NONE) {
1830 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
1831 pthread_mutex_unlock(&account_mutex);
1832 return ret_transaction;
1835 *account_id = _account_get_next_sequence(ACCOUNT_TABLE);
1836 data->id = *account_id;
1839 appid = _account_get_current_appid(pid);
1844 // API caller cannot be recognized
1845 ret_transaction = _account_end_transaction(FALSE);
1846 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1847 pthread_mutex_unlock(&account_mutex);
1848 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
1852 char* verified_appid = NULL;
1853 error_code = _account_check_account_type_with_appid_group(appid, &verified_appid);//FIX
1854 _ACCOUNT_FREE(appid);
1855 if(error_code != ACCOUNT_ERROR_NONE)
1857 _ERR("error_code = %d", error_code);
1858 ret_transaction = _account_end_transaction(FALSE);
1859 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1860 _ACCOUNT_FREE(verified_appid);
1861 pthread_mutex_unlock(&account_mutex);
1868 error_code = _account_check_duplicated(data, verified_appid);
1869 if (error_code != ACCOUNT_ERROR_NONE) {
1871 ret_transaction = _account_end_transaction(FALSE);
1872 ACCOUNT_DEBUG("_account_check_duplicated(), rollback insert query(%x)!!!!\n", ret_transaction);
1874 pthread_mutex_unlock(&account_mutex);
1877 if(!_account_check_add_more_account(verified_appid)) {
1878 ret_transaction = _account_end_transaction(FALSE);
1879 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
1880 pthread_mutex_unlock(&account_mutex);
1881 _ACCOUNT_FREE(verified_appid);
1882 return ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
1885 _ACCOUNT_FREE(data->package_name);
1886 data->package_name = _account_get_text(verified_appid);
1887 _ACCOUNT_FREE(verified_appid);
1890 if(!_account_check_add_more_account(data->package_name))
1893 ret_transaction = _account_end_transaction(FALSE);
1894 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
1895 pthread_mutex_unlock(&account_mutex);
1896 return ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
1899 error_code = _account_execute_insert_query(data);
1901 if (error_code != ACCOUNT_ERROR_NONE)
1904 ret_transaction = _account_end_transaction(FALSE);
1905 ACCOUNT_ERROR("INSERT account fail, rollback insert query(%x)!!!!\n", ret_transaction);
1907 pthread_mutex_unlock(&account_mutex);
1912 error_code = _account_insert_capability(data, *account_id);
1913 if (error_code != ACCOUNT_ERROR_NONE)
1916 ret_transaction = _account_end_transaction(FALSE);
1917 ACCOUNT_ERROR("INSERT capability fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1919 pthread_mutex_unlock(&account_mutex);
1924 error_code = _account_insert_custom(data, *account_id);
1925 if (error_code != ACCOUNT_ERROR_NONE)
1927 ret_transaction = _account_end_transaction(FALSE);
1928 ACCOUNT_ERROR("INSERT custom fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1930 pthread_mutex_unlock(&account_mutex);
1936 pthread_mutex_unlock(&account_mutex);
1937 _account_end_transaction(TRUE);
1938 ACCOUNT_SLOGD("(%s)-(%d) account _end_transaction.\n", __FUNCTION__, __LINE__);
1941 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_INSERT, *account_id);
1942 _account_insert_delete_update_notification_send(buf);
1943 _INFO("account _notification_send end.");
1945 return ACCOUNT_ERROR_NONE;
1949 int _account_query_capability_by_account_id(capability_cb callback, int account_id, void *user_data )
1951 int error_code = ACCOUNT_ERROR_NONE;
1952 account_stmt hstmt = NULL;
1953 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1956 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1957 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
1958 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1960 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1962 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
1963 hstmt = _account_prepare_query(query);
1965 if( _account_db_err_code() == SQLITE_PERM ){
1966 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1967 return ACCOUNT_ERROR_PERMISSION_DENIED;
1970 rc = _account_query_step(hstmt);
1971 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1973 account_capability_s* capability_record = NULL;
1975 while (rc == SQLITE_ROW) {
1976 bool cb_ret = FALSE;
1977 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
1979 if (capability_record == NULL) {
1980 ACCOUNT_FATAL("malloc Failed");
1984 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
1986 _account_convert_column_to_capability(hstmt, capability_record);
1988 cb_ret = callback(capability_record->type, capability_record->value, user_data);
1990 _account_free_capability_items(capability_record);
1991 _ACCOUNT_FREE(capability_record);
1993 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
1995 rc = _account_query_step(hstmt);
1998 rc = _account_query_finalize(hstmt);
1999 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2002 error_code = ACCOUNT_ERROR_NONE;
2005 if (hstmt != NULL) {
2006 rc = _account_query_finalize(hstmt);
2007 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2011 pthread_mutex_unlock(&account_mutex);
2015 GSList* _account_get_capability_list_by_account_id(int account_id, int *error_code)
2017 *error_code = ACCOUNT_ERROR_NONE;
2018 account_stmt hstmt = NULL;
2019 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2021 GSList* capability_list = NULL;
2023 ACCOUNT_RETURN_VAL((account_id > 0), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("ACCOUNT INDEX IS LESS THAN 0"));
2024 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
2026 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2028 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2029 hstmt = _account_prepare_query(query);
2031 if( _account_db_err_code() == SQLITE_PERM ){
2032 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2033 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
2037 rc = _account_query_step(hstmt);
2038 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2040 account_capability_s* capability_record = NULL;
2042 while (rc == SQLITE_ROW) {
2043 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
2045 if (capability_record == NULL) {
2046 ACCOUNT_FATAL("malloc Failed");
2050 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
2052 _account_convert_column_to_capability(hstmt, capability_record);
2054 //cb_ret = callback(capability_record->type, capability_record->value, user_data);
2056 //_account_free_capability_items(capability_record);
2057 //_ACCOUNT_FREE(capability_record);
2059 //ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
2061 capability_list = g_slist_append(capability_list, capability_record);
2062 rc = _account_query_step(hstmt);
2065 rc = _account_query_finalize(hstmt);
2066 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
2069 *error_code = ACCOUNT_ERROR_NONE;
2074 rc = _account_query_finalize(hstmt);
2075 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
2079 pthread_mutex_unlock(&account_mutex);
2080 return capability_list;
2083 static int _account_compare_old_record(account_s *new_account, int account_id)
2085 int error_code = ACCOUNT_ERROR_NONE;
2086 account_stmt hstmt = NULL;
2087 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2089 account_s *old_account = NULL;
2091 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2092 ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2093 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2095 old_account = (account_s*)calloc(1, sizeof(account_s));
2097 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2099 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2100 hstmt = _account_prepare_query(query);
2102 rc = _account_query_step(hstmt);
2103 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2105 while (rc == SQLITE_ROW) {
2106 _account_convert_column_to_account(hstmt, old_account);
2107 rc = _account_query_step(hstmt);
2110 rc = _account_query_finalize(hstmt);
2111 ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2115 error_code = _account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account);
2116 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
2119 error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account);
2120 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
2124 new_account->id = old_account->id;
2127 if(!new_account->user_name) {
2128 if(old_account->user_name)
2129 new_account->user_name = _account_get_text(old_account->user_name);
2133 if(!new_account->display_name) {
2134 if(old_account->display_name)
2135 new_account->display_name = _account_get_text(old_account->display_name);
2139 if(!new_account->email_address) {
2140 if(old_account->email_address)
2141 new_account->email_address = _account_get_text(old_account->email_address);
2145 if(!new_account->domain_name) {
2146 if(old_account->domain_name)
2147 new_account->domain_name = _account_get_text(old_account->domain_name);
2151 if(!new_account->icon_path) {
2152 if(old_account->icon_path)
2153 new_account->icon_path = _account_get_text(old_account->icon_path);
2157 if(!new_account->source) {
2158 if(old_account->source)
2159 new_account->source = _account_get_text(old_account->source);
2162 _ACCOUNT_FREE(new_account->package_name);
2163 new_account->package_name = _account_get_text(old_account->package_name);
2166 if(!new_account->access_token) {
2167 if(old_account->access_token)
2168 new_account->access_token = _account_get_text(old_account->access_token);
2173 for(i=0;i<USER_TXT_CNT;i++) {
2174 if(!new_account->user_data_txt[i]) {
2175 if(old_account->user_data_txt[i])
2176 new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]);
2181 if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) {
2182 new_account->auth_type = old_account->auth_type;
2186 if(new_account->secret== ACCOUNT_SECRECY_INVALID) {
2187 new_account->secret = old_account->secret;
2191 if(new_account->sync_support == ACCOUNT_SYNC_INVALID) {
2192 new_account->sync_support = old_account->sync_support;
2196 for(i=0;i<USER_INT_CNT;i++) {
2197 if(new_account->user_data_int[i] == 0) {
2198 new_account->user_data_int[i] = old_account->user_data_int[i];
2204 // user custom table
2208 _account_free_account_items(old_account);
2209 _ACCOUNT_FREE(old_account);
2212 if (hstmt != NULL) {
2213 rc = _account_query_finalize(hstmt);
2214 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2218 return ACCOUNT_ERROR_NONE;
2221 static int _account_get_package_name_from_account_id(int account_id, char **package_name)
2223 int error_code = ACCOUNT_ERROR_NONE;
2224 account_stmt hstmt = NULL;
2225 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2227 account_s *old_account = NULL;
2229 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2230 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2232 old_account = (account_s*)calloc(1, sizeof(account_s));
2234 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2236 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2237 hstmt = _account_prepare_query(query);
2239 rc = _account_query_step(hstmt);
2240 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2242 while (rc == SQLITE_ROW) {
2243 _account_convert_column_to_account(hstmt, old_account);
2244 rc = _account_query_step(hstmt);
2247 rc = _account_query_finalize(hstmt);
2248 ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2251 // get package name.
2252 *package_name = _account_get_text(old_account->package_name);
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"));
2271 static int _account_update_account(int pid, account_s *account, int account_id)
2273 int rc = 0, binding_count =0;
2274 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2275 int error_code = ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0;
2276 account_stmt hstmt = NULL;
2278 if (!account->package_name) {
2279 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
2280 return ACCOUNT_ERROR_INVALID_PARAMETER;
2283 /* Check permission of requested appid */
2284 char* current_appid = NULL;
2285 char *package_name = NULL;
2287 current_appid = _account_get_current_appid(pid);
2288 error_code = _account_get_package_name_from_account_id(account_id, &package_name);
2290 if(error_code != ACCOUNT_ERROR_NONE || package_name == NULL){
2291 ACCOUNT_ERROR("No package name with account_id\n");
2292 _ACCOUNT_FREE(current_appid);
2293 _ACCOUNT_FREE(package_name);
2294 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2297 error_code = _account_check_appid_group_with_package_name(current_appid, package_name);
2298 ACCOUNT_DEBUG( "UPDATE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name); // TODO: remove the log later.
2300 _ACCOUNT_FREE(current_appid);
2301 _ACCOUNT_FREE(package_name);
2303 if(error_code != ACCOUNT_ERROR_NONE){
2304 ACCOUNT_ERROR("No permission to update\n");
2305 return ACCOUNT_ERROR_PERMISSION_DENIED;
2308 _account_compare_old_record(account, account_id);
2310 if( _account_db_err_code() == SQLITE_PERM ){
2311 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2312 return ACCOUNT_ERROR_PERMISSION_DENIED;
2313 } else if( _account_db_err_code() == SQLITE_BUSY ){
2314 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
2315 return ACCOUNT_ERROR_DATABASE_BUSY;
2318 if (!account->user_name && !account->display_name && !account->email_address) {
2319 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
2320 return ACCOUNT_ERROR_INVALID_PARAMETER;
2323 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2325 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
2327 count = _account_get_record_count(query);
2329 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
2330 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2333 /* transaction control required*/
2334 ret_transaction = _account_begin_transaction();
2335 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
2336 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
2337 pthread_mutex_unlock(&account_mutex);
2338 return ACCOUNT_ERROR_DATABASE_BUSY;
2341 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2342 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
2343 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
2344 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
2345 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
2347 hstmt = _account_prepare_query(query);
2349 if( _account_db_err_code() == SQLITE_PERM ){
2350 ret_transaction = _account_end_transaction(FALSE);
2351 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2352 return ACCOUNT_ERROR_PERMISSION_DENIED;
2355 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)));
2357 binding_count = _account_convert_account_to_sql(account, hstmt, query);
2358 _account_query_bind_int(hstmt, binding_count++, account_id);
2360 rc = _account_query_step(hstmt);
2361 if (rc != SQLITE_DONE) {
2362 ACCOUNT_SLOGE( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2365 rc = _account_query_finalize(hstmt);
2366 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2369 _INFO("update query=%s", query);
2371 /*update capability*/
2372 error_code = _account_update_capability(account, account_id);
2373 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2374 ret_transaction = _account_end_transaction(FALSE);
2375 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2380 error_code = _account_update_custom(account, account_id);
2381 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2382 ret_transaction = _account_end_transaction(FALSE);
2383 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2387 ret_transaction = _account_end_transaction(TRUE);
2389 _INFO("update end");
2394 static int _account_update_account_ex(account_s *account, int account_id)
2396 int rc = 0, binding_count =0;
2397 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2398 int error_code = ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0;
2399 account_stmt hstmt = NULL;
2401 if (!account->package_name) {
2402 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
2403 return ACCOUNT_ERROR_INVALID_PARAMETER;
2406 _account_compare_old_record(account, account_id);
2408 if( _account_db_err_code() == SQLITE_PERM ){
2409 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2410 return ACCOUNT_ERROR_PERMISSION_DENIED;
2413 if (!account->user_name && !account->display_name && !account->email_address) {
2414 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
2415 return ACCOUNT_ERROR_INVALID_PARAMETER;
2418 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2420 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
2422 count = _account_get_record_count(query);
2424 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
2425 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2428 /* transaction control required*/
2429 ret_transaction = _account_begin_transaction();
2430 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
2431 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
2432 pthread_mutex_unlock(&account_mutex);
2433 return ACCOUNT_ERROR_DATABASE_BUSY;
2436 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2437 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
2438 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
2439 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
2440 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
2442 hstmt = _account_prepare_query(query);
2444 if( _account_db_err_code() == SQLITE_PERM ){
2445 ret_transaction = _account_end_transaction(FALSE);
2446 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2447 return ACCOUNT_ERROR_PERMISSION_DENIED;
2450 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)));
2452 _INFO("account_update_to_db_by_id_ex_p : before convert() : account_id[%d], user_name=%s", account->id, account->user_name);
2453 binding_count = _account_convert_account_to_sql(account, hstmt, query);
2454 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], user_name=%s", account->id, account->user_name);
2455 _INFO("account_update_to_db_by_id_ex_p : before bind()");
2456 rc = _account_query_bind_int(hstmt, binding_count++, account_id);
2457 _INFO("account_update_to_db_by_id_ex_p : after bind() : ret = %d", rc);
2459 rc = _account_query_step(hstmt);
2460 if (rc != SQLITE_DONE) {
2461 ACCOUNT_SLOGE( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2463 _INFO("account_update_to_db_by_id_ex_p : after query_step() : ret = %d", rc);
2465 rc = _account_query_finalize(hstmt);
2466 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2468 _INFO("account_update_to_db_by_id_ex_p : after query_filnalize() : ret = %d", rc);
2470 _INFO("account_update_to_db_by_id_ex_p : before update_capability()");
2471 /*update capability*/
2472 error_code = _account_update_capability(account, account_id);
2473 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2474 ret_transaction = _account_end_transaction(FALSE);
2475 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2478 _INFO("account_update_to_db_by_id_ex_p : after update_capability()");
2480 _INFO("account_update_to_db_by_id_ex_p : before update_custom()");
2482 error_code = _account_update_custom(account, account_id);
2483 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2484 ret_transaction = _account_end_transaction(FALSE);
2485 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2488 _INFO("account_update_to_db_by_id_ex_p : after update_custom()");
2490 ret_transaction = _account_end_transaction(TRUE);
2496 int _account_update_to_db_by_id(int pid, account_s* account, int account_id)
2498 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
2499 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
2500 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2501 int error_code = ACCOUNT_ERROR_NONE;
2502 account_s* data = (account_s*)account;
2504 pthread_mutex_lock(&account_mutex);
2506 error_code = _account_update_account(pid, data, account_id);
2508 if(error_code != ACCOUNT_ERROR_NONE) {
2509 pthread_mutex_unlock(&account_mutex);
2513 pthread_mutex_unlock(&account_mutex);
2516 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, account_id);
2517 _account_insert_delete_update_notification_send(buf);
2519 return ACCOUNT_ERROR_NONE;
2522 int _account_update_to_db_by_id_ex(account_s* account, int account_id)
2524 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
2525 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
2526 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2527 int error_code = ACCOUNT_ERROR_NONE;
2528 account_s* data = account;
2530 pthread_mutex_lock(&account_mutex);
2532 _INFO("before update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
2533 error_code = _account_update_account_ex(data, account_id);
2534 _INFO("after update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
2536 if(error_code != ACCOUNT_ERROR_NONE) {
2537 pthread_mutex_unlock(&account_mutex);
2541 pthread_mutex_unlock(&account_mutex);
2544 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, account_id);
2545 _account_insert_delete_update_notification_send(buf);
2547 return ACCOUNT_ERROR_NONE;
2551 int _account_update_to_db_by_user_name(int pid, account_s* account, const char *user_name, const char *package_name)
2553 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
2554 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
2555 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2557 int error_code = ACCOUNT_ERROR_NONE;
2558 account_s *data = (account_s*)account;
2560 pthread_mutex_lock(&account_mutex);
2562 error_code = _account_update_account_by_user_name(pid, data, user_name, package_name);
2564 pthread_mutex_unlock(&account_mutex);
2567 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, data->id);
2568 _account_insert_delete_update_notification_send(buf);
2573 GSList* _account_db_query_all(int pid)
2575 //int error_code = ACCOUNT_ERROR_NONE;
2576 account_stmt hstmt = NULL;
2577 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2579 GSList *account_list = NULL;
2581 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
2583 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2585 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TABLE);
2586 hstmt = _account_prepare_query(query);
2588 if( _account_db_err_code() == SQLITE_PERM ){
2589 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2593 rc = _account_query_step(hstmt);
2595 account_s *account_record = NULL;
2597 if (rc != SQLITE_ROW)
2599 _ERR("The record isn't found");
2603 while(rc == SQLITE_ROW) {
2604 account_record = (account_s*) malloc(sizeof(account_s));
2606 if (account_record == NULL) {
2607 ACCOUNT_FATAL("malloc Failed");
2611 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2612 _account_convert_column_to_account(hstmt, account_record);
2613 account_list = g_slist_append(account_list, account_record);
2614 rc = _account_query_step(hstmt);
2617 rc = _account_query_finalize(hstmt);
2618 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, NULL, ("finalize error"));
2623 for (iter = account_list; iter != NULL; iter = g_slist_next(iter)) {
2624 account_s *account = NULL;
2625 account = (account_s*)iter->data;
2626 _account_query_capability_by_account_id(_account_get_capability_text_cb, account->id, (void*)account);
2627 _account_query_custom_by_account_id(_account_get_custom_text_cb, account->id, (void*)account);
2631 if (hstmt != NULL) {
2632 rc = _account_query_finalize(hstmt);
2633 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {_account_gslist_free(account_list);}, NULL, ("finalize error"));
2638 _remove_sensitive_info_from_non_owning_account_slist(pid, account_list);
2640 return account_list;
2643 int _account_update_sync_status_by_id(int account_db_id, const int sync_status)
2645 int error_code = ACCOUNT_ERROR_NONE;
2646 account_stmt hstmt = NULL;
2647 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2651 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2652 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2653 if ( (sync_status < 0) || (sync_status > ACCOUNT_SYNC_STATUS_RUNNING)) {
2654 ACCOUNT_SLOGE("(%s)-(%d) sync_status is less than 0 or more than enum max.\n", __FUNCTION__, __LINE__);
2655 return ACCOUNT_ERROR_INVALID_PARAMETER;
2658 pthread_mutex_lock(&account_mutex);
2660 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2662 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_db_id);
2664 rc = _account_get_record_count(query);
2666 if( _account_db_err_code() == SQLITE_PERM ){
2667 pthread_mutex_unlock(&account_mutex);
2668 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2669 return ACCOUNT_ERROR_PERMISSION_DENIED;
2673 ACCOUNT_SLOGE( "account_update_sync_status_by_id : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
2674 pthread_mutex_unlock(&account_mutex);
2675 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2678 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2680 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET sync_support=? WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
2681 hstmt = _account_prepare_query(query);
2683 _account_query_bind_int(hstmt, count, sync_status);
2685 rc = _account_query_step(hstmt);
2687 if( _account_db_err_code() == SQLITE_PERM ){
2688 pthread_mutex_unlock(&account_mutex);
2689 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2690 return ACCOUNT_ERROR_PERMISSION_DENIED;
2693 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_DB_FAILED,
2694 ("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()));
2696 rc = _account_query_finalize(hstmt);
2697 if (rc != ACCOUNT_ERROR_NONE) {
2698 ACCOUNT_ERROR("_account_query_finalize error");
2699 pthread_mutex_unlock(&account_mutex);
2703 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_SYNC_UPDATE, account_db_id);
2704 _account_insert_delete_update_notification_send(buf);
2707 error_code = ACCOUNT_ERROR_NONE;
2710 if (hstmt != NULL) {
2711 rc = _account_query_finalize(hstmt);
2712 pthread_mutex_unlock(&account_mutex);
2713 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2717 pthread_mutex_unlock(&account_mutex);
2721 int _account_query_account_by_account_id(int pid, int account_db_id, account_s *account_record)
2723 _INFO("_account_query_account_by_account_id() start, account_db_id=[%d]", account_db_id);
2725 int error_code = ACCOUNT_ERROR_NONE;
2726 account_stmt hstmt = NULL;
2727 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2730 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2731 ACCOUNT_RETURN_VAL(account_record != NULL, {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2732 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2734 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2736 ACCOUNT_DEBUG("starting db operations");
2738 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
2739 hstmt = _account_prepare_query(query);
2740 rc = _account_db_err_code();
2741 _INFO("after _account_prepare_query, rc=[%d]", rc);
2743 if( rc == SQLITE_PERM ){
2744 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2745 return ACCOUNT_ERROR_PERMISSION_DENIED;
2748 ACCOUNT_DEBUG("before _account_query_step");
2749 rc = _account_query_step(hstmt);
2750 ACCOUNT_DEBUG("after _account_query_step returned [%d]", rc);
2751 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2753 while (rc == SQLITE_ROW) {
2754 ACCOUNT_DEBUG("before _account_convert_column_to_account");
2755 _account_convert_column_to_account(hstmt, account_record);
2756 ACCOUNT_DEBUG("after _account_convert_column_to_account");
2757 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]);
2758 rc = _account_query_step(hstmt);
2761 ACCOUNT_DEBUG("account_record->id=[%d]", account_record->id);
2763 rc = _account_query_finalize(hstmt);
2764 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2766 ACCOUNT_DEBUG("before _account_query_capability_by_account_id");
2767 _account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
2768 ACCOUNT_DEBUG("after _account_query_capability_by_account_id");
2770 ACCOUNT_DEBUG("before _account_query_custom_by_account_id");
2771 _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record);
2772 ACCOUNT_DEBUG("after _account_query_custom_by_account_id");
2775 error_code = ACCOUNT_ERROR_NONE;
2778 if (hstmt != NULL) {
2779 rc = _account_query_finalize(hstmt);
2780 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2786 _remove_sensitive_info_from_non_owning_account(pid, account_record);
2788 pthread_mutex_unlock(&account_mutex);
2789 ACCOUNT_DEBUG("_account_query_account_by_account_id end [%d]", error_code);
2793 GList* _account_query_account_by_user_name(int pid, const char *user_name, int *error_code)
2795 *error_code = ACCOUNT_ERROR_NONE;
2796 account_stmt hstmt = NULL;
2797 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2799 account_s *account_head = NULL;
2801 if (user_name == NULL)
2803 _ERR("USER NAME IS NULL");
2804 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
2808 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2810 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ?", ACCOUNT_TABLE);
2812 hstmt = _account_prepare_query(query);
2814 if (_account_db_err_code() == SQLITE_PERM)
2816 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2817 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
2821 int binding_count = 1;
2822 _account_query_bind_text(hstmt, binding_count++, user_name);
2824 rc = _account_query_step(hstmt);
2826 if (rc != SQLITE_ROW)
2828 _ERR("The record isn't found");
2829 *error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
2835 account_head = (account_s*) malloc(sizeof(account_s));
2836 if (account_head == NULL) {
2837 ACCOUNT_FATAL("malloc Failed");
2838 if (hstmt != NULL) {
2839 rc = _account_query_finalize(hstmt);
2841 if (rc != ACCOUNT_ERROR_NONE)
2843 _ERR("finalize error");
2849 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
2852 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2854 while (rc == SQLITE_ROW) {
2855 account_s* account_record = NULL;
2857 account_record = (account_s*) malloc(sizeof(account_s));
2859 if (account_record == NULL) {
2860 ACCOUNT_FATAL("malloc Failed");
2863 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2865 _account_convert_column_to_account(hstmt, account_record);
2867 account_head->account_list = g_list_append(account_head->account_list, account_record);
2869 rc = _account_query_step(hstmt);
2873 rc = _account_query_finalize(hstmt);
2875 if (rc != ACCOUNT_ERROR_NONE)
2877 _ERR("finalize error");
2887 tmp = g_list_length(account_head->account_list);
2889 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2891 account = (account_h)iter->data;
2893 account_s *testaccount = (account_s*)account;
2895 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
2896 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
2900 *error_code = ACCOUNT_ERROR_NONE;
2903 if (hstmt != NULL) {
2904 rc = _account_query_finalize(hstmt);
2905 if (rc != ACCOUNT_ERROR_NONE)
2907 _ERR("finalize error");
2913 pthread_mutex_unlock(&account_mutex);
2916 _remove_sensitive_info_from_non_owning_account_list(pid, account_head->account_list);
2917 GList* result = account_head->account_list;
2918 _ACCOUNT_FREE(account_head);
2925 _account_query_account_by_capability(int pid, const char* capability_type, const int capability_value, int *error_code)
2927 *error_code = ACCOUNT_ERROR_NONE;
2928 account_stmt hstmt = NULL;
2929 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2932 ACCOUNT_RETURN_VAL((capability_type != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("capability_type IS NULL"));
2934 if ((capability_value < 0) || (capability_value > ACCOUNT_CAPABILITY_ENABLED)) {
2935 ACCOUNT_SLOGE("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
2936 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
2940 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
2942 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2944 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=? AND value=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
2946 hstmt = _account_prepare_query(query);
2948 if( _account_db_err_code() == SQLITE_PERM ){
2949 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2950 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
2954 int binding_count = 1;
2955 _account_query_bind_text(hstmt, binding_count++, capability_type);
2956 _account_query_bind_int(hstmt, binding_count++, capability_value);
2958 rc = _account_query_step(hstmt);
2960 account_s* account_head = NULL;
2962 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2966 account_head = (account_s*) malloc(sizeof(account_s));
2967 if (account_head == NULL) {
2968 ACCOUNT_FATAL("malloc Failed");
2969 if (hstmt != NULL) {
2970 rc = _account_query_finalize(hstmt);
2971 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
2974 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
2977 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2979 while (rc == SQLITE_ROW) {
2980 account_s* account_record = NULL;
2982 account_record = (account_s*) malloc(sizeof(account_s));
2984 if (account_record == NULL) {
2985 ACCOUNT_FATAL("malloc Failed");
2988 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2990 _account_convert_column_to_account(hstmt, account_record);
2992 account_head->account_list = g_list_append(account_head->account_list, account_record);
2994 rc = _account_query_step(hstmt);
2998 rc = _account_query_finalize(hstmt);
2999 ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3005 tmp = g_list_length(account_head->account_list);
3007 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3008 account_h account = NULL;
3009 account = (account_h)iter->data;
3010 account_s* testaccount = (account_s*)account;
3012 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3013 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3018 *error_code = ACCOUNT_ERROR_NONE;
3021 if (hstmt != NULL) {
3022 rc = _account_query_finalize(hstmt);
3023 if ( rc != ACCOUNT_ERROR_NONE ) {
3025 _ERR("finalize error");
3030 if( *error_code != ACCOUNT_ERROR_NONE && account_head ) {
3031 _account_glist_free(account_head->account_list);
3032 _ACCOUNT_FREE(account_head);
3033 account_head = NULL;
3036 pthread_mutex_unlock(&account_mutex);
3040 _remove_sensitive_info_from_non_owning_account_list(pid, account_head->account_list);
3041 GList* result = account_head->account_list;
3042 _ACCOUNT_FREE(account_head);
3048 GList* _account_query_account_by_capability_type(int pid, const char* capability_type, int *error_code)
3050 *error_code = ACCOUNT_ERROR_NONE;
3051 account_stmt hstmt = NULL;
3052 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3055 ACCOUNT_RETURN_VAL((capability_type != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("capability_type IS NULL"));
3056 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;},
3057 NULL, ("The database isn't connected."));
3059 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3061 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
3063 hstmt = _account_prepare_query(query);
3065 if( _account_db_err_code() == SQLITE_PERM ){
3066 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3067 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
3071 int binding_count = 1;
3072 _account_query_bind_text(hstmt, binding_count++, capability_type);
3074 rc = _account_query_step(hstmt);
3076 account_s* account_head = NULL;
3078 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3082 account_head = (account_s*) malloc(sizeof(account_s));
3083 if (account_head == NULL) {
3084 ACCOUNT_FATAL("malloc Failed");
3085 if (hstmt != NULL) {
3086 rc = _account_query_finalize(hstmt);
3087 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
3090 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
3093 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3095 while (rc == SQLITE_ROW) {
3096 account_s* account_record = NULL;
3098 account_record = (account_s*) malloc(sizeof(account_s));
3100 if (account_record == NULL) {
3101 ACCOUNT_FATAL("malloc Failed");
3104 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3106 _account_convert_column_to_account(hstmt, account_record);
3108 account_head->account_list = g_list_append(account_head->account_list, account_record);
3110 rc = _account_query_step(hstmt);
3114 rc = _account_query_finalize(hstmt);
3115 ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3121 tmp = g_list_length(account_head->account_list);
3123 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3124 account_s* testaccount = (account_s*)iter->data;
3126 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3127 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3131 *error_code = ACCOUNT_ERROR_NONE;
3136 rc = _account_query_finalize(hstmt);
3137 if (rc != ACCOUNT_ERROR_NONE) {
3139 _ERR("finalize error");
3144 if( (*error_code != ACCOUNT_ERROR_NONE) && account_head ) {
3145 _account_glist_free(account_head->account_list);
3146 _ACCOUNT_FREE(account_head);
3147 account_head = NULL;
3150 pthread_mutex_unlock(&account_mutex);
3154 _remove_sensitive_info_from_non_owning_account_list(pid, account_head->account_list);
3155 GList* result = account_head->account_list;
3156 _ACCOUNT_FREE(account_head);
3162 GList* _account_query_account_by_package_name(int pid,const char* package_name, int *error_code)
3164 _INFO("_account_query_account_by_package_name");
3166 *error_code = ACCOUNT_ERROR_NONE;
3167 account_stmt hstmt = NULL;
3168 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3171 ACCOUNT_RETURN_VAL((package_name != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("PACKAGE NAME IS NULL"));
3172 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
3174 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3176 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE package_name=?", ACCOUNT_TABLE);
3178 hstmt = _account_prepare_query(query);
3180 if( _account_db_err_code() == SQLITE_PERM ){
3181 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3182 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
3186 int binding_count = 1;
3187 _account_query_bind_text(hstmt, binding_count++, package_name);
3189 rc = _account_query_step(hstmt);
3191 account_s* account_head = NULL;
3193 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.(%s)\n", package_name));
3197 account_head = (account_s*) malloc(sizeof(account_s));
3198 if (account_head == NULL) {
3199 ACCOUNT_FATAL("malloc Failed");
3200 if (hstmt != NULL) {
3201 rc = _account_query_finalize(hstmt);
3202 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
3205 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
3208 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3210 while (rc == SQLITE_ROW) {
3211 account_s* account_record = NULL;
3213 account_record = (account_s*) malloc(sizeof(account_s));
3215 if (account_record == NULL) {
3216 ACCOUNT_FATAL("malloc Failed");
3219 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3221 _account_convert_column_to_account(hstmt, account_record);
3223 _INFO("Adding account_list");
3224 account_head->account_list = g_list_append(account_head->account_list, account_record);
3226 rc = _account_query_step(hstmt);
3230 rc = _account_query_finalize(hstmt);
3231 ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3236 tmp = g_list_length(account_head->account_list);
3238 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3239 account_s* testaccount = (account_s*)iter->data;
3241 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3242 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3245 *error_code = ACCOUNT_ERROR_NONE;
3250 rc = _account_query_finalize(hstmt);
3251 if (rc != ACCOUNT_ERROR_NONE) {
3253 _ERR("finalize error");
3258 pthread_mutex_unlock(&account_mutex);
3260 if( (*error_code != ACCOUNT_ERROR_NONE) && account_head ) {
3261 _account_glist_free(account_head->account_list);
3262 _ACCOUNT_FREE(account_head);
3263 account_head = NULL;
3266 if ((*error_code == ACCOUNT_ERROR_NONE) && account_head != NULL)
3268 _INFO("Returning account_list");
3269 _remove_sensitive_info_from_non_owning_account_list(pid,account_head->account_list);
3270 GList* result = account_head->account_list;
3271 _ACCOUNT_FREE(account_head);
3277 int _account_delete(int pid, int account_id)
3279 int error_code = ACCOUNT_ERROR_NONE;
3280 account_stmt hstmt = NULL;
3281 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3283 int ret_transaction = 0;
3284 bool is_success = FALSE;
3286 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3289 /* Check requested ID to delete */
3290 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id=%d", ACCOUNT_TABLE, account_id);
3292 count = _account_get_record_count(query);
3294 if( _account_db_err_code() == SQLITE_PERM ){
3295 pthread_mutex_unlock(&account_mutex);
3296 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3297 return ACCOUNT_ERROR_PERMISSION_DENIED;
3301 ACCOUNT_ERROR("account id(%d) is not exist. count(%d)\n", account_id, count);
3302 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3305 /* Check permission of requested appid */
3306 char* current_appid = NULL;
3307 char *package_name = NULL;
3309 current_appid = _account_get_current_appid(pid);
3311 error_code = _account_get_package_name_from_account_id(account_id, &package_name);
3313 if(error_code != ACCOUNT_ERROR_NONE){
3314 ACCOUNT_ERROR("No package name with account_id\n");
3315 _ACCOUNT_FREE(current_appid);
3316 _ACCOUNT_FREE(package_name);
3317 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3319 ACCOUNT_DEBUG( "DELETE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name);
3321 error_code = _account_check_appid_group_with_package_name(current_appid, package_name);
3323 _ACCOUNT_FREE(current_appid);
3324 _ACCOUNT_FREE(package_name);
3326 if(error_code != ACCOUNT_ERROR_NONE){
3327 ACCOUNT_ERROR("No permission to delete\n");
3328 return ACCOUNT_ERROR_PERMISSION_DENIED;
3331 /* transaction control required*/
3332 ret_transaction = _account_begin_transaction();
3334 if( _account_db_err_code() == SQLITE_PERM ){
3335 pthread_mutex_unlock(&account_mutex);
3336 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3337 return ACCOUNT_ERROR_PERMISSION_DENIED;
3340 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
3341 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
3342 pthread_mutex_unlock(&account_mutex);
3343 return ACCOUNT_ERROR_DATABASE_BUSY;
3346 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3347 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3348 pthread_mutex_unlock(&account_mutex);
3349 return ret_transaction;
3352 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3353 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
3355 hstmt = _account_prepare_query(query);
3357 if( _account_db_err_code() == SQLITE_PERM ){
3358 pthread_mutex_unlock(&account_mutex);
3359 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3360 return ACCOUNT_ERROR_PERMISSION_DENIED;
3363 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3364 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3366 rc = _account_query_step(hstmt);
3367 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3369 rc = _account_query_finalize(hstmt);
3371 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3374 ACCOUNT_MEMSET(query, 0, sizeof(query));
3376 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
3378 hstmt = _account_prepare_query(query);
3379 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3380 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3382 rc = _account_query_step(hstmt);
3383 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
3385 rc = _account_query_finalize(hstmt);
3386 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3389 /* delete custom data */
3390 ACCOUNT_MEMSET(query, 0, sizeof(query));
3392 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
3394 hstmt = _account_prepare_query(query);
3396 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3397 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3399 rc = _account_query_step(hstmt);
3400 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
3402 rc = _account_query_finalize(hstmt);
3403 ACCOUNT_CATCH_ERROR(rc == ACCOUNT_ERROR_NONE, {}, rc, ("finalize error", account_id, rc));
3409 if (hstmt != NULL) {
3410 rc = _account_query_finalize(hstmt);
3411 if(rc != ACCOUNT_ERROR_NONE ){
3412 ACCOUNT_ERROR("rc (%d)", rc);
3419 ret_transaction = _account_end_transaction(is_success);
3421 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3422 ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3424 if (is_success == true) {
3426 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id);
3427 _account_insert_delete_update_notification_send(buf);
3431 pthread_mutex_unlock(&account_mutex);
3437 static int _account_query_account_by_username_and_package(const char* username, const char* package_name, account_h *account)
3441 int error_code = ACCOUNT_ERROR_NONE;
3442 account_stmt hstmt = NULL;
3443 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3445 int binding_count = 1;
3447 ACCOUNT_RETURN_VAL((username != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("username IS NULL"));
3448 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name IS NULL"));
3449 ACCOUNT_RETURN_VAL((*account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
3450 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3452 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3454 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
3455 hstmt = _account_prepare_query(query);
3457 if( _account_db_err_code() == SQLITE_PERM ){
3458 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3459 return ACCOUNT_ERROR_PERMISSION_DENIED;
3462 _account_query_bind_text(hstmt, binding_count++, username);
3463 _account_query_bind_text(hstmt, binding_count++, package_name);
3465 rc = _account_query_step(hstmt);
3466 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3468 account_s *account_record = (account_s *)(*account);
3470 while (rc == SQLITE_ROW) {
3471 _account_convert_column_to_account(hstmt, account_record);
3472 rc = _account_query_step(hstmt);
3475 rc = _account_query_finalize(hstmt);
3476 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3477 _account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
3478 _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record);
3481 error_code = ACCOUNT_ERROR_NONE;
3484 if (hstmt != NULL) {
3485 rc = _account_query_finalize(hstmt);
3486 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3490 pthread_mutex_unlock(&account_mutex);
3494 int _account_create(account_h *account)
3497 ACCOUNT_SLOGE("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
3498 return ACCOUNT_ERROR_INVALID_PARAMETER;
3501 account_s *data = (account_s*)malloc(sizeof(account_s));
3504 ACCOUNT_FATAL("Memory Allocation Failed");
3505 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3507 ACCOUNT_MEMSET(data, 0, sizeof(account_s));
3509 /*Setting account as visible by default*/
3510 data->secret = ACCOUNT_SECRECY_VISIBLE;
3512 /*Setting account as not supporting sync by default*/
3513 data->sync_support = ACCOUNT_SYNC_NOT_SUPPORT;
3515 *account = (account_h)data;
3517 return ACCOUNT_ERROR_NONE;
3520 int _account_destroy(account_h account)
3522 account_s *data = (account_s*)account;
3524 ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!"));
3526 _account_free_account_items(data);
3527 _ACCOUNT_FREE(data);
3529 return ACCOUNT_ERROR_NONE;
3532 int _account_get_account_id(account_s* account, int *account_id)
3535 return ACCOUNT_ERROR_INVALID_PARAMETER;
3538 return ACCOUNT_ERROR_INVALID_PARAMETER;
3541 *account_id = account->id;
3543 return ACCOUNT_ERROR_NONE;
3546 int _account_delete_from_db_by_user_name(int pid, const char *user_name, const char *package_name)
3548 _INFO("[%s][%s]", user_name, package_name);
3550 int error_code = ACCOUNT_ERROR_NONE;
3551 account_stmt hstmt = NULL;
3552 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3554 int ret_transaction = 0;
3555 bool is_success = FALSE;
3556 account_h account = NULL;
3557 int binding_count = 1;
3558 int account_id = -1;
3560 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is null!"));
3561 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
3562 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3564 /* Check permission of requested appid */
3565 char* current_appid = NULL;
3566 char* package_name_temp = NULL;
3568 current_appid = _account_get_current_appid(pid);
3570 package_name_temp = _account_get_text(package_name);
3572 ACCOUNT_DEBUG( "DELETE:user_name[%s],current_appid[%s], package_name[%s]", user_name, current_appid, package_name_temp);
3574 error_code = _account_check_appid_group_with_package_name(current_appid, package_name_temp);
3576 _ACCOUNT_FREE(current_appid);
3577 _ACCOUNT_FREE(package_name_temp);
3579 if(error_code != ACCOUNT_ERROR_NONE){
3580 ACCOUNT_ERROR("No permission to delete\n");
3581 return ACCOUNT_ERROR_PERMISSION_DENIED;
3584 rc = _account_create(&account);
3585 rc = _account_query_account_by_username_and_package(user_name, package_name, &account);
3589 if( _account_db_err_code() == SQLITE_PERM )
3591 _account_destroy(account);
3592 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3593 return ACCOUNT_ERROR_PERMISSION_DENIED;
3597 account_s* account_data = (account_s*)account;
3599 rc = _account_get_account_id(account_data, &account_id);
3601 rc = _account_destroy(account);
3603 /* transaction control required*/
3604 ret_transaction = _account_begin_transaction();
3606 if( _account_db_err_code() == SQLITE_PERM )
3608 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3609 pthread_mutex_unlock(&account_mutex);
3610 return ACCOUNT_ERROR_PERMISSION_DENIED;
3614 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY )
3616 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
3617 pthread_mutex_unlock(&account_mutex);
3618 return ACCOUNT_ERROR_DATABASE_BUSY;
3620 else if (ret_transaction != ACCOUNT_ERROR_NONE) {
3621 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3622 pthread_mutex_unlock(&account_mutex);
3623 return ret_transaction;
3626 /* delete custom data */
3627 ACCOUNT_MEMSET(query, 0, sizeof(query));
3628 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = ?", ACCOUNT_CUSTOM_TABLE);
3630 hstmt = _account_prepare_query(query);
3632 if( _account_db_err_code() == SQLITE_PERM ){
3633 _account_end_transaction(FALSE);
3634 pthread_mutex_unlock(&account_mutex);
3635 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3636 return ACCOUNT_ERROR_PERMISSION_DENIED;
3639 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3640 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3642 _account_query_bind_int(hstmt, binding_count++, account_id);
3644 rc = _account_query_step(hstmt);
3645 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3647 rc = _account_query_finalize(hstmt);
3648 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3651 /* delete capability */
3652 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", CAPABILITY_TABLE);
3654 hstmt = _account_prepare_query(query);
3656 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3657 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3660 _account_query_bind_text(hstmt, binding_count++, user_name);
3661 _account_query_bind_text(hstmt, binding_count++, package_name);
3663 rc = _account_query_step(hstmt);
3664 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3666 rc = _account_query_finalize(hstmt);
3667 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3670 ACCOUNT_MEMSET(query, 0, sizeof(query));
3673 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
3675 hstmt = _account_prepare_query(query);
3676 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3677 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3681 _account_query_bind_text(hstmt, binding_count++, user_name);
3682 _account_query_bind_text(hstmt, binding_count++, package_name);
3684 rc = _account_query_step(hstmt);
3685 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));
3687 rc = _account_query_finalize(hstmt);
3688 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3694 if (hstmt != NULL) {
3695 rc = _account_query_finalize(hstmt);
3696 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3700 ret_transaction = _account_end_transaction(is_success);
3702 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3703 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3705 if (is_success == true) {
3707 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id);
3708 _account_insert_delete_update_notification_send(buf);
3712 pthread_mutex_unlock(&account_mutex);
3717 int _account_delete_from_db_by_package_name(int pid, const char *package_name, gboolean permission)
3719 _INFO("_account_delete_from_db_by_package_name");
3720 int error_code = ACCOUNT_ERROR_NONE;
3721 account_stmt hstmt = NULL;
3722 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3724 int ret_transaction = 0;
3725 bool is_success = FALSE;
3726 int binding_count = 1;
3727 GSList *account_id_list = NULL;
3730 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
3731 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3733 /* Check permission of requested appid */
3735 char* current_appid = NULL;
3736 char* package_name_temp = NULL;
3738 current_appid = _account_get_current_appid(pid);
3740 package_name_temp = _account_get_text(package_name);
3742 ACCOUNT_DEBUG( "DELETE: current_appid[%s], package_name[%s]", current_appid, package_name_temp);
3744 error_code = _account_check_appid_group_with_package_name(current_appid, package_name_temp);
3746 _ACCOUNT_FREE(current_appid);
3747 _ACCOUNT_FREE(package_name_temp);
3749 if(error_code != ACCOUNT_ERROR_NONE){
3750 ACCOUNT_ERROR("No permission to delete\n");
3751 return ACCOUNT_ERROR_PERMISSION_DENIED;
3755 // It only needs list of ids, does not need to query sensitive info. So sending 0
3756 GList* account_list_temp = _account_query_account_by_package_name(getpid(), package_name, &ret);
3757 if( _account_db_err_code() == SQLITE_PERM ){
3758 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3759 return ACCOUNT_ERROR_PERMISSION_DENIED;
3762 if(ret != ACCOUNT_ERROR_NONE){
3763 _ERR("_account_query_account_by_package_name failed ret=[%d]", ret);
3767 account_list_temp = g_list_first(account_list_temp);
3768 _INFO("account_list_temp length=[%d]",g_list_length(account_list_temp));
3771 for (iter = account_list_temp; iter != NULL; iter = g_list_next(iter))
3773 _INFO("iterating account_list_temp");
3774 account_s *account = NULL;
3775 _INFO("Before iter->data");
3776 account = (account_s*)iter->data;
3777 _INFO("After iter->data");
3778 if (account != NULL)
3780 char id[256] = {0, };
3782 ACCOUNT_MEMSET(id, 0, 256);
3784 ACCOUNT_SNPRINTF(id, 256, "%d", account->id);
3786 _INFO("Adding account id [%s]", id);
3787 account_id_list = g_slist_append(account_id_list, g_strdup(id));
3791 /* transaction control required*/
3792 ret_transaction = _account_begin_transaction();
3794 if( _account_db_err_code() == SQLITE_PERM ){
3795 pthread_mutex_unlock(&account_mutex);
3796 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3797 return ACCOUNT_ERROR_PERMISSION_DENIED;
3800 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
3801 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
3802 pthread_mutex_unlock(&account_mutex);
3803 return ACCOUNT_ERROR_DATABASE_BUSY;
3804 }else if (ret_transaction != ACCOUNT_ERROR_NONE) {
3805 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3806 pthread_mutex_unlock(&account_mutex);
3807 return ret_transaction;
3810 /* delete custom table */
3811 ACCOUNT_MEMSET(query, 0, sizeof(query));
3812 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", ACCOUNT_CUSTOM_TABLE);
3814 hstmt = _account_prepare_query(query);
3816 if( _account_db_err_code() == SQLITE_PERM ){
3817 _account_end_transaction(FALSE);
3818 pthread_mutex_unlock(&account_mutex);
3819 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3820 return ACCOUNT_ERROR_PERMISSION_DENIED;
3823 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3824 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3827 _account_query_bind_text(hstmt, binding_count++, package_name);
3829 rc = _account_query_step(hstmt);
3830 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3832 rc = _account_query_finalize(hstmt);
3833 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3836 /* delete capability table */
3837 ACCOUNT_MEMSET(query, 0, sizeof(query));
3838 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", CAPABILITY_TABLE);
3840 hstmt = _account_prepare_query(query);
3842 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3843 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3846 _account_query_bind_text(hstmt, binding_count++, package_name);
3848 rc = _account_query_step(hstmt);
3849 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3851 rc = _account_query_finalize(hstmt);
3852 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3855 /* delete account table */
3856 ACCOUNT_MEMSET(query, 0, sizeof(query));
3858 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", ACCOUNT_TABLE);
3860 hstmt = _account_prepare_query(query);
3861 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3862 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3865 _account_query_bind_text(hstmt, binding_count++, package_name);
3867 rc = _account_query_step(hstmt);
3868 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));
3870 rc = _account_query_finalize(hstmt);
3871 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3877 if (hstmt != NULL) {
3878 rc = _account_query_finalize(hstmt);
3879 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3883 ret_transaction = _account_end_transaction(is_success);
3885 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3886 ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3888 if (is_success == true) {
3889 GSList* gs_iter = NULL;
3890 for (gs_iter = account_id_list; gs_iter != NULL; gs_iter = g_slist_next(gs_iter)) {
3891 char* p_tmpid = NULL;
3892 p_tmpid = (char*)gs_iter->data;
3894 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%s", ACCOUNT_NOTI_NAME_DELETE, p_tmpid);
3895 ACCOUNT_SLOGD("%s", buf);
3896 _account_insert_delete_update_notification_send(buf);
3897 _ACCOUNT_FREE(p_tmpid);
3899 g_slist_free(account_id_list);
3903 pthread_mutex_unlock(&account_mutex);
3905 _INFO("_account_delete_from_db_by_package_name end");
3909 int _account_get_total_count_from_db(gboolean include_hidden, int *count)
3912 ACCOUNT_SLOGE("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__);
3913 return ACCOUNT_ERROR_INVALID_PARAMETER;
3917 ACCOUNT_ERROR("DB is not opened\n");
3918 return ACCOUNT_ERROR_DB_NOT_OPENED;
3921 char query[1024] = {0, };
3922 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3926 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s", ACCOUNT_TABLE);
3930 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where secret = %d", ACCOUNT_TABLE, ACCOUNT_SECRECY_VISIBLE);
3933 *count = _account_get_record_count(query);
3935 if( _account_db_err_code() == SQLITE_PERM ){
3936 pthread_mutex_unlock(&account_mutex);
3937 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3938 return ACCOUNT_ERROR_PERMISSION_DENIED;
3943 account_stmt pStmt = NULL;
3945 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
3946 if (SQLITE_OK != rc) {
3947 ACCOUNT_SLOGE("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg());
3948 sqlite3_finalize(pStmt);
3949 return ACCOUNT_ERROR_DB_FAILED;
3952 rc = sqlite3_step(pStmt);
3953 if (SQLITE_ROW != rc) {
3954 ACCOUNT_ERROR("[ERROR] sqlite3_step() failed\n");
3955 sqlite3_finalize(pStmt);
3956 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3959 ncount = sqlite3_column_int(pStmt, 0);
3963 sqlite3_finalize(pStmt);
3966 ACCOUNT_ERROR("[ERROR] Number of account : %d, End", ncount);
3967 return ACCOUNT_ERROR_DB_FAILED;
3970 return ACCOUNT_ERROR_NONE;
3973 int account_type_create(account_type_h *account_type)
3975 if (!account_type) {
3976 ACCOUNT_SLOGE("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
3977 return ACCOUNT_ERROR_INVALID_PARAMETER;
3980 account_type_s *data = (account_type_s*)malloc(sizeof(account_type_s));
3983 ACCOUNT_ERROR("Memory Allocation Failed");
3984 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3987 ACCOUNT_MEMSET(data, 0, sizeof(account_type_s));
3989 *account_type = (account_type_h)data;
3991 return ACCOUNT_ERROR_NONE;
3994 int account_type_destroy(account_type_h account_type)
3996 account_type_s *data = (account_type_s*)account_type;
3998 ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account type handle is null!"));
4000 _account_type_free_account_type_items(data);
4001 _ACCOUNT_FREE(data);
4003 return ACCOUNT_ERROR_NONE;
4006 //app_id mandatory field
4007 int account_type_set_app_id(account_type_h account_type, const char *app_id)
4009 if (!account_type) {
4010 ACCOUNT_SLOGE("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
4011 return ACCOUNT_ERROR_INVALID_PARAMETER;
4015 ACCOUNT_SLOGE("(%s)-(%d) app_id is NULL.\n", __FUNCTION__, __LINE__);
4016 return ACCOUNT_ERROR_INVALID_PARAMETER;
4019 account_type_s *data = (account_type_s*)account_type;
4021 _ACCOUNT_FREE(data->app_id);
4022 data->app_id = _account_get_text(app_id);
4024 return ACCOUNT_ERROR_NONE;
4027 //service_provider_id mandatory field
4028 int account_type_set_service_provider_id(account_type_h account_type, const char *service_provider_id)
4030 if (!account_type) {
4031 return ACCOUNT_ERROR_INVALID_PARAMETER;
4034 if (!service_provider_id) {
4035 return ACCOUNT_ERROR_INVALID_PARAMETER;
4038 account_type_s *data = (account_type_s*)account_type;
4040 _ACCOUNT_FREE(data->service_provider_id);
4041 data->service_provider_id = _account_get_text(service_provider_id);
4043 return ACCOUNT_ERROR_NONE;
4046 int account_type_set_icon_path(account_type_h account_type, const char *icon_path)
4048 if (!account_type) {
4049 return ACCOUNT_ERROR_INVALID_PARAMETER;
4053 return ACCOUNT_ERROR_INVALID_PARAMETER;
4056 account_type_s *data = (account_type_s*)account_type;
4058 _ACCOUNT_FREE(data->icon_path);
4059 data->icon_path = _account_get_text(icon_path);
4061 return ACCOUNT_ERROR_NONE;
4064 int account_type_set_small_icon_path(account_type_h account_type, const char *small_icon_path)
4066 if (!account_type) {
4067 return ACCOUNT_ERROR_INVALID_PARAMETER;
4070 if (!small_icon_path) {
4071 return ACCOUNT_ERROR_INVALID_PARAMETER;
4074 account_type_s *data = (account_type_s*)account_type;
4076 _ACCOUNT_FREE(data->small_icon_path);
4077 data->small_icon_path = _account_get_text(small_icon_path);
4079 return ACCOUNT_ERROR_NONE;
4082 int account_type_set_multiple_account_support(account_type_h account_type, const bool multiple_account_support)
4084 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
4086 account_type_s *data = (account_type_s*)account_type;
4088 data->multiple_account_support = multiple_account_support;
4090 return ACCOUNT_ERROR_NONE;
4094 int account_type_set_label(account_type_h account_type, const char* label, const char* locale)
4096 if (!account_type) {
4097 ACCOUNT_SLOGE("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
4098 return ACCOUNT_ERROR_INVALID_PARAMETER;
4101 if(!label || !locale) {
4102 return ACCOUNT_ERROR_INVALID_PARAMETER;
4105 account_type_s *data = (account_type_s*)account_type;
4106 label_s *label_data = (label_s*)malloc(sizeof(label_s));
4108 if (label_data == NULL) {
4109 return ACCOUNT_ERROR_OUT_OF_MEMORY;
4111 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
4113 label_data->label = _account_get_text(label);
4114 label_data->locale = _account_get_text(locale);
4116 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
4118 return ACCOUNT_ERROR_NONE;
4121 int account_type_get_app_id(account_type_h account_type, char **app_id)
4123 if (!account_type) {
4124 return ACCOUNT_ERROR_INVALID_PARAMETER;
4128 return ACCOUNT_ERROR_INVALID_PARAMETER;
4131 account_type_s *data = (account_type_s*)account_type;
4134 *app_id = _account_get_text(data->app_id);
4136 return ACCOUNT_ERROR_NONE;
4139 int account_type_get_service_provider_id(account_type_h account_type, char **service_provider_id)
4141 if (!account_type) {
4142 return ACCOUNT_ERROR_INVALID_PARAMETER;
4145 if (!service_provider_id) {
4146 return ACCOUNT_ERROR_INVALID_PARAMETER;
4149 account_type_s *data = (account_type_s*)account_type;
4151 (*service_provider_id) = NULL;
4152 *service_provider_id = _account_get_text(data->service_provider_id);
4154 return ACCOUNT_ERROR_NONE;
4157 int account_type_get_icon_path(account_type_h account_type, char **icon_path)
4159 if (!account_type) {
4160 return ACCOUNT_ERROR_INVALID_PARAMETER;
4164 return ACCOUNT_ERROR_INVALID_PARAMETER;
4167 account_type_s *data = (account_type_s*)account_type;
4169 (*icon_path) = NULL;
4170 *icon_path = _account_get_text(data->icon_path);
4172 return ACCOUNT_ERROR_NONE;
4175 int account_type_get_small_icon_path(account_type_h account_type, char **small_icon_path)
4177 if (!account_type) {
4178 return ACCOUNT_ERROR_INVALID_PARAMETER;
4181 if (!small_icon_path) {
4182 return ACCOUNT_ERROR_INVALID_PARAMETER;
4185 account_type_s *data = (account_type_s*)account_type;
4187 (*small_icon_path) = NULL;
4188 *small_icon_path = _account_get_text(data->small_icon_path);
4190 return ACCOUNT_ERROR_NONE;
4193 int account_type_get_multiple_account_support(account_type_h account_type, int *multiple_account_support)
4195 if (!account_type) {
4196 return ACCOUNT_ERROR_INVALID_PARAMETER;
4198 if (!multiple_account_support) {
4199 return ACCOUNT_ERROR_INVALID_PARAMETER;
4202 account_type_s *data = (account_type_s*)account_type;
4204 *multiple_account_support = data->multiple_account_support;
4206 return ACCOUNT_ERROR_NONE;
4209 int account_type_get_label_by_locale(account_type_h account_type, const char* locale, char** label)
4211 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4212 ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID PARAMETER"));
4215 account_type_s *data = (account_type_s*)account_type;
4217 for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) {
4218 label_s *label_data = NULL;
4220 label_data = (label_s*)iter->data;
4224 if(!strcmp(locale, label_data->locale)) {
4225 *label = _account_get_text(label_data->label);
4226 return ACCOUNT_ERROR_NONE;
4230 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4233 int account_type_get_label(account_type_h account_type, account_label_cb callback, void *user_data)
4235 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4236 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4239 account_type_s *data = (account_type_s*)account_type;
4241 for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) {
4242 label_s *label_data = NULL;
4244 label_data = (label_s*)iter->data;
4246 if(callback(label_data->app_id, label_data->label, label_data->locale, user_data)!=TRUE) {
4247 ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n");
4248 return ACCOUNT_ERROR_NONE;
4252 return ACCOUNT_ERROR_NONE;
4255 static gboolean _account_type_check_duplicated(account_type_s *data)
4257 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4260 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4262 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId='%s'"
4263 , ACCOUNT_TYPE_TABLE, data->app_id);
4265 count = _account_get_record_count(query);
4273 static int _account_type_convert_account_to_sql(account_type_s *account_type, account_stmt hstmt, char *sql_value)
4279 /*Caution : Keep insert query orders.*/
4282 _account_query_bind_text(hstmt, count++, (char*)account_type->app_id);
4284 /* 2. service provider id*/
4285 _account_query_bind_text(hstmt, count++, (char*)account_type->service_provider_id);
4288 _account_query_bind_text(hstmt, count++, (char*)account_type->icon_path);
4290 /* 4. small icon path*/
4291 _account_query_bind_text(hstmt, count++, (char*)account_type->small_icon_path);
4293 /* 5. multiple accont support*/
4294 _account_query_bind_int(hstmt, count++, account_type->multiple_account_support);
4302 static int _account_type_execute_insert_query(account_type_s *account_type)
4307 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4308 int error_code = ACCOUNT_ERROR_NONE;
4309 account_stmt hstmt = NULL;
4311 /* check mandatory field */
4312 // app id & service provider id
4313 if (!account_type->app_id) {
4314 return ACCOUNT_ERROR_INVALID_PARAMETER;
4317 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4318 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s( AppId, ServiceProviderId , IconPath , SmallIconPath , MultipleAccountSupport ) values "
4319 "(?, ?, ?, ?, ?)", ACCOUNT_TYPE_TABLE);
4322 hstmt = _account_prepare_query(query);
4325 if( _account_db_err_code() == SQLITE_PERM ){
4326 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4327 return ACCOUNT_ERROR_PERMISSION_DENIED;
4328 } else if( _account_db_err_code() == SQLITE_BUSY ){
4329 ACCOUNT_ERROR( "Database Busy(%s)", _account_db_err_msg());
4330 return ACCOUNT_ERROR_DATABASE_BUSY;
4333 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4336 _account_type_convert_account_to_sql(account_type, hstmt, query);
4339 rc = _account_query_step(hstmt);
4340 if (rc == SQLITE_BUSY) {
4341 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4342 error_code = ACCOUNT_ERROR_DATABASE_BUSY;
4343 } else if (rc != SQLITE_DONE) {
4344 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4345 error_code = ACCOUNT_ERROR_DB_FAILED;
4349 rc = _account_query_finalize(hstmt);
4350 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4357 static int _account_type_insert_label(account_type_s *account_type)
4360 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4361 account_stmt hstmt = NULL;
4363 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4365 if (g_slist_length( account_type->label_list)==0) {
4366 ACCOUNT_ERROR( "_account_type_insert_label, no label\n");
4367 return ACCOUNT_ERROR_NONE;
4370 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId = '%s'", ACCOUNT_TYPE_TABLE, account_type->app_id);
4372 rc = _account_get_record_count(query);
4374 if( _account_db_err_code() == SQLITE_PERM ){
4375 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4376 return ACCOUNT_ERROR_PERMISSION_DENIED;
4380 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4386 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
4389 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4390 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
4391 "(?, ?, ?) ", LABEL_TABLE);
4393 hstmt = _account_prepare_query(query);
4395 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4397 label_s* label_data = NULL;
4398 label_data = (label_s*)iter->data;
4400 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4401 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4402 ret = _account_query_bind_text(hstmt, count++, label_data->label);
4403 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4404 ret = _account_query_bind_text(hstmt, count++, (char*)label_data->locale);
4405 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4407 rc = _account_query_step(hstmt);
4409 if (rc != SQLITE_DONE) {
4410 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4414 rc = _account_query_finalize(hstmt);
4415 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4420 return ACCOUNT_ERROR_NONE;
4423 static void _account_type_convert_column_to_provider_feature(account_stmt hstmt, provider_feature_s *feature_record)
4425 const char *textbuf = NULL;
4427 textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_APP_ID);
4428 _account_db_data_to_text(textbuf, &(feature_record->app_id));
4430 textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_KEY);
4431 _account_db_data_to_text(textbuf, &(feature_record->key));
4435 GSList* _account_type_query_provider_feature_by_app_id(const char* app_id, int *error_code)
4437 _INFO("_account_type_query_provider_feature_by_app_id");
4438 *error_code = ACCOUNT_ERROR_NONE;
4439 account_stmt hstmt = NULL;
4440 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4441 int rc = 0, binding_count = 1;
4442 GSList* feature_list = NULL;
4444 ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
4445 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
4447 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4449 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
4450 _INFO("account query=[%s]", query);
4452 hstmt = _account_prepare_query(query);
4454 if( _account_db_err_code() == SQLITE_PERM ){
4455 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4456 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
4460 _account_query_bind_text(hstmt, binding_count++, app_id);
4462 rc = _account_query_step(hstmt);
4463 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {*error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;}, NULL, ("The record isn't found.\n"));
4465 provider_feature_s* feature_record = NULL;
4467 while (rc == SQLITE_ROW) {
4469 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
4471 if (feature_record == NULL) {
4472 ACCOUNT_FATAL("malloc Failed");
4476 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
4478 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
4480 _INFO("Adding account feature_list");
4481 feature_list = g_slist_append(feature_list, feature_record);
4483 rc = _account_query_step(hstmt);
4486 rc = _account_query_finalize(hstmt);
4487 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("account finalize error"));
4490 *error_code = ACCOUNT_ERROR_NONE;
4493 if (hstmt != NULL) {
4494 rc = _account_query_finalize(hstmt);
4495 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("account finalize error"));
4499 pthread_mutex_unlock(&account_mutex);
4501 _INFO("Returning account feature_list");
4502 return feature_list;
4505 int account_type_query_provider_feature_by_app_id(provider_feature_cb callback, const char* app_id, void *user_data )
4507 int error_code = ACCOUNT_ERROR_NONE;
4508 account_stmt hstmt = NULL;
4509 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4510 int rc = 0, binding_count = 1;
4512 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4513 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4514 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4516 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4518 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
4519 hstmt = _account_prepare_query(query);
4521 if( _account_db_err_code() == SQLITE_PERM ){
4522 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4523 return ACCOUNT_ERROR_PERMISSION_DENIED;
4526 _account_query_bind_text(hstmt, binding_count++, app_id);
4528 rc = _account_query_step(hstmt);
4529 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4531 provider_feature_s* feature_record = NULL;
4533 while (rc == SQLITE_ROW) {
4534 bool cb_ret = FALSE;
4535 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
4537 if (feature_record == NULL) {
4538 ACCOUNT_FATAL("malloc Failed");
4542 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
4544 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
4546 cb_ret = callback(feature_record->app_id, feature_record->key, user_data);
4548 _account_type_free_feature_items(feature_record);
4549 _ACCOUNT_FREE(feature_record);
4551 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
4553 rc = _account_query_step(hstmt);
4556 rc = _account_query_finalize(hstmt);
4557 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4560 error_code = ACCOUNT_ERROR_NONE;
4563 if (hstmt != NULL) {
4564 rc = _account_query_finalize(hstmt);
4565 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4569 pthread_mutex_unlock(&account_mutex);
4573 bool _account_type_query_supported_feature(const char* app_id, const char* capability, int *error_code)
4575 _INFO("_account_type_query_supported_feature start");
4577 *error_code = ACCOUNT_ERROR_NONE;
4579 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4580 int record_count = 0;
4582 if (app_id == NULL || capability == NULL)
4584 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
4588 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
4590 record_count = _account_get_record_count(query);
4592 if( _account_db_err_code() == SQLITE_PERM ){
4593 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4594 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
4598 if (record_count <= 0)
4600 *error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
4604 _INFO("_account_type_query_supported_feature end");
4610 int account_type_get_provider_feature_all(account_type_h account_type, provider_feature_cb callback, void* user_data)
4612 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4613 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4616 account_type_s *data = (account_type_s*)account_type;
4618 for (iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4619 provider_feature_s *feature_data = NULL;
4621 feature_data = (provider_feature_s*)iter->data;
4623 if(callback(feature_data->app_id, feature_data->key, user_data)!=TRUE) {
4624 ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n");
4625 return ACCOUNT_ERROR_NONE;
4629 return ACCOUNT_ERROR_NONE;
4632 int account_type_set_provider_feature(account_type_h account_type, const char* provider_feature)
4634 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account type handle is null"));
4635 ACCOUNT_RETURN_VAL((provider_feature != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("provider_feature is null"));
4637 account_type_s *data = (account_type_s*)account_type;
4639 GSList *iter = NULL;
4640 bool b_is_new = TRUE;
4642 for(iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4643 provider_feature_s *feature_data = NULL;
4644 feature_data = (provider_feature_s*)iter->data;
4646 if(!strcmp(feature_data->key, provider_feature)) {
4653 provider_feature_s* feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
4655 if (feature_data == NULL)
4656 return ACCOUNT_ERROR_OUT_OF_MEMORY;
4657 ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
4659 feature_data->key = _account_get_text(provider_feature);
4660 data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
4663 return ACCOUNT_ERROR_NONE;
4666 static int _account_type_insert_provider_feature(account_type_s *account_type, const char* app_id)
4669 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4670 account_stmt hstmt = NULL;
4672 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4673 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4675 if (g_slist_length( account_type->provider_feature_list)==0) {
4676 ACCOUNT_ERROR( "no capability\n");
4677 return ACCOUNT_ERROR_NONE;
4680 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId='%s'", ACCOUNT_TYPE_TABLE, app_id);
4682 rc = _account_get_record_count(query);
4684 if( _account_db_err_code() == SQLITE_PERM ){
4685 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4686 return ACCOUNT_ERROR_PERMISSION_DENIED;
4690 ACCOUNT_SLOGI( "related account type item is not existed rc=%d , %s", rc, _account_db_err_msg());
4691 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4698 for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4701 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4702 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
4703 "(?, ?) ", PROVIDER_FEATURE_TABLE);
4705 hstmt = _account_prepare_query(query);
4707 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4709 provider_feature_s* feature_data = NULL;
4710 feature_data = (provider_feature_s*)iter->data;
4712 ret = _account_query_bind_text(hstmt, count++, app_id);
4713 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4714 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
4715 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
4717 rc = _account_query_step(hstmt);
4719 if (rc != SQLITE_DONE) {
4720 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4724 rc = _account_query_finalize(hstmt);
4725 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4730 return ACCOUNT_ERROR_NONE;
4733 int _account_type_insert_to_db(account_type_s* account_type, int* account_type_id)
4737 int error_code = ACCOUNT_ERROR_NONE, ret_transaction = 0;
4739 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4740 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL"));
4741 ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID POINTER IS NULL"));
4743 account_type_s *data = (account_type_s*)account_type;
4745 pthread_mutex_lock(&account_mutex);
4748 /* transaction control required*/
4749 ret_transaction = _account_begin_transaction();
4753 if( _account_db_err_code() == SQLITE_PERM ){
4754 pthread_mutex_unlock(&account_mutex);
4755 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4756 return ACCOUNT_ERROR_PERMISSION_DENIED;
4760 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
4761 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
4762 pthread_mutex_unlock(&account_mutex);
4763 return ACCOUNT_ERROR_DATABASE_BUSY;
4764 } else if (ret_transaction != ACCOUNT_ERROR_NONE) {
4765 ACCOUNT_ERROR("_account_begin_transaction fail %d\n", ret_transaction);
4766 pthread_mutex_unlock(&account_mutex);
4767 return ret_transaction;
4771 if (_account_type_check_duplicated(data)) {
4773 ret_transaction = _account_end_transaction(FALSE);
4774 ACCOUNT_ERROR("Duplicated, rollback insert query(%x)!!!!\n", ret_transaction);
4775 *account_type_id = -1;
4776 pthread_mutex_unlock(&account_mutex);
4777 return ACCOUNT_ERROR_DUPLICATED;
4780 *account_type_id = _account_get_next_sequence(ACCOUNT_TYPE_TABLE);
4782 error_code = _account_type_execute_insert_query(data);
4784 if (error_code != ACCOUNT_ERROR_NONE){
4785 error_code = ACCOUNT_ERROR_DUPLICATED;
4786 ret_transaction = _account_end_transaction(FALSE);
4787 ACCOUNT_ERROR("Insert fail, rollback insert query(%x)!!!!\n", ret_transaction);
4788 *account_type_id = -1;
4789 pthread_mutex_unlock(&account_mutex);
4795 error_code = _account_type_insert_provider_feature(data, data->app_id);
4796 if(error_code != ACCOUNT_ERROR_NONE) {
4798 ret_transaction = _account_end_transaction(FALSE);
4799 ACCOUNT_ERROR("Insert provider feature fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
4800 pthread_mutex_unlock(&account_mutex);
4804 error_code = _account_type_insert_label(data);
4805 if(error_code != ACCOUNT_ERROR_NONE) {
4807 ret_transaction = _account_end_transaction(FALSE);
4808 ACCOUNT_ERROR("Insert label fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
4809 pthread_mutex_unlock(&account_mutex);
4813 ret_transaction = _account_end_transaction(TRUE);
4815 pthread_mutex_unlock(&account_mutex);
4818 return ACCOUNT_ERROR_NONE;
4821 static int _account_type_update_provider_feature(account_type_s *account_type, const char* app_id)
4824 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4825 account_stmt hstmt = NULL;
4827 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4829 if (g_slist_length( account_type->provider_feature_list)==0) {
4830 ACCOUNT_ERROR( "no feature\n");
4831 return ACCOUNT_ERROR_NONE;
4834 ACCOUNT_DEBUG( "app id", app_id);
4836 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4838 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id=? ", PROVIDER_FEATURE_TABLE);
4839 hstmt = _account_prepare_query(query);
4841 if( _account_db_err_code() == SQLITE_PERM ){
4842 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4843 return ACCOUNT_ERROR_PERMISSION_DENIED;
4847 _account_query_bind_text(hstmt, count++, app_id);
4848 rc = _account_query_step(hstmt);
4850 if (rc != SQLITE_DONE) {
4851 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4852 return ACCOUNT_ERROR_DB_FAILED;
4854 rc = _account_query_finalize(hstmt);
4855 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4860 for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4863 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4864 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
4865 "(?, ?) ", PROVIDER_FEATURE_TABLE);
4867 hstmt = _account_prepare_query(query);
4869 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4871 provider_feature_s* feature_data = NULL;
4872 feature_data = (provider_feature_s*)iter->data;
4874 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4875 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4876 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
4877 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4879 rc = _account_query_step(hstmt);
4881 if (rc != SQLITE_DONE) {
4882 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4885 rc = _account_query_finalize(hstmt);
4886 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4890 return ACCOUNT_ERROR_NONE;
4893 static int _account_type_update_label(account_type_s *account_type, const char* app_id)
4896 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4897 account_stmt hstmt = NULL;
4899 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4901 if (g_slist_length( account_type->label_list)==0) {
4902 return ACCOUNT_ERROR_NONE;
4905 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4907 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId=? ", LABEL_TABLE);
4908 hstmt = _account_prepare_query(query);
4910 if( _account_db_err_code() == SQLITE_PERM ){
4911 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4912 return ACCOUNT_ERROR_PERMISSION_DENIED;
4916 _account_query_bind_text(hstmt, count++, app_id);
4917 rc = _account_query_step(hstmt);
4919 if (rc != SQLITE_DONE) {
4920 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4921 return ACCOUNT_ERROR_DB_FAILED;
4923 rc = _account_query_finalize(hstmt);
4924 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4929 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
4932 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4933 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
4934 "(?, ?, ?) ", LABEL_TABLE);
4936 hstmt = _account_prepare_query(query);
4938 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4940 label_s* label_data = NULL;
4941 label_data = (label_s*)iter->data;
4943 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4944 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4945 ret = _account_query_bind_text(hstmt, count++, label_data->label);
4946 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4947 ret = _account_query_bind_text(hstmt, count++, label_data->locale);
4948 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4950 rc = _account_query_step(hstmt);
4952 if (rc != SQLITE_DONE) {
4953 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4956 rc = _account_query_finalize(hstmt);
4957 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4961 return ACCOUNT_ERROR_NONE;
4965 static int _account_type_update_account(account_type_s *account_type, const char* app_id)
4967 int rc = 0, binding_count =1;
4968 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4969 int error_code = ACCOUNT_ERROR_NONE;
4970 account_stmt hstmt = NULL;
4972 if (!account_type->app_id) {
4973 ACCOUNT_ERROR("app id is mandetory field, it can not be NULL!!!!\n");
4974 return ACCOUNT_ERROR_INVALID_PARAMETER;
4977 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4978 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET AppId=?, ServiceProviderId=?, IconPath=?, "
4979 "SmallIconPath=?, MultipleAccountSupport=? WHERE AppId=? ", ACCOUNT_TYPE_TABLE);
4981 hstmt = _account_prepare_query(query);
4983 if( _account_db_err_code() == SQLITE_PERM ){
4984 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4985 return ACCOUNT_ERROR_PERMISSION_DENIED;
4986 } else if (_account_db_err_code() == SQLITE_BUSY){
4987 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
4988 return ACCOUNT_ERROR_DATABASE_BUSY;
4991 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
4993 binding_count = _account_type_convert_account_to_sql(account_type, hstmt, query);
4994 _account_query_bind_text(hstmt, binding_count++, app_id);
4996 rc = _account_query_step(hstmt);
4997 if (rc != SQLITE_DONE) {
4998 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5001 rc = _account_query_finalize(hstmt);
5002 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5006 error_code = _account_type_update_label(account_type, app_id);
5007 /* update provider feature */
5008 error_code = _account_type_update_provider_feature(account_type, app_id);
5013 int _account_type_update_to_db_by_app_id(account_type_s* account_type, const char* app_id)
5015 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
5016 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5017 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5019 int error_code = ACCOUNT_ERROR_NONE;
5020 account_type_s* data = account_type;
5022 pthread_mutex_lock(&account_mutex);
5024 error_code = _account_type_update_account(data, app_id);
5026 pthread_mutex_unlock(&account_mutex);
5031 int _account_type_delete_by_app_id(const char* app_id)
5033 int error_code = ACCOUNT_ERROR_NONE;
5034 account_stmt hstmt = NULL;
5035 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5036 int rc = 0, count = -1;
5037 int ret_transaction = 0;
5038 int binding_count = 1;
5039 bool is_success = FALSE;
5041 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5042 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("The database isn't connected."));
5044 /* Check requested ID to delete */
5045 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
5047 count = _account_get_record_count(query);
5049 if( _account_db_err_code() == SQLITE_PERM ){
5050 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5051 return ACCOUNT_ERROR_PERMISSION_DENIED;
5055 ACCOUNT_SLOGE("app id(%s) is not exist. count(%d)\n", app_id, count);
5056 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5059 /* transaction control required*/
5060 ret_transaction = _account_begin_transaction();
5062 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
5063 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
5064 pthread_mutex_unlock(&account_mutex);
5065 return ACCOUNT_ERROR_DATABASE_BUSY;
5066 }else if (ret_transaction != ACCOUNT_ERROR_NONE) {
5067 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
5068 pthread_mutex_unlock(&account_mutex);
5069 return ret_transaction;
5072 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", LABEL_TABLE);
5074 hstmt = _account_prepare_query(query);
5076 if( _account_db_err_code() == SQLITE_PERM ){
5077 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5078 pthread_mutex_unlock(&account_mutex);
5079 return ACCOUNT_ERROR_PERMISSION_DENIED;
5082 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
5083 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
5085 _account_query_bind_text(hstmt, binding_count++, app_id);
5087 rc = _account_query_step(hstmt);
5088 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5090 rc = _account_query_finalize(hstmt);
5091 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5095 ACCOUNT_MEMSET(query, 0, sizeof(query));
5097 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id = ? ", PROVIDER_FEATURE_TABLE);
5099 hstmt = _account_prepare_query(query);
5100 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
5101 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
5103 _account_query_bind_text(hstmt, binding_count++, app_id);
5105 rc = _account_query_step(hstmt);
5106 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
5108 rc = _account_query_finalize(hstmt);
5109 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5115 ACCOUNT_MEMSET(query, 0, sizeof(query));
5117 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ? ", ACCOUNT_TYPE_TABLE);
5119 hstmt = _account_prepare_query(query);
5120 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
5121 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
5123 _account_query_bind_text(hstmt, binding_count++, app_id);
5125 rc = _account_query_step(hstmt);
5126 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
5128 rc = _account_query_finalize(hstmt);
5129 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5135 if (hstmt != NULL) {
5136 rc = _account_query_finalize(hstmt);
5137 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5141 ret_transaction = _account_end_transaction(is_success);
5143 if (ret_transaction != ACCOUNT_ERROR_NONE) {
5144 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
5147 pthread_mutex_unlock(&account_mutex);
5152 static void _account_type_convert_column_to_account_type(account_stmt hstmt, account_type_s *account_type_record)
5154 const char *textbuf = NULL;
5156 account_type_record->id = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_ID);
5158 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_APP_ID);
5159 _account_db_data_to_text(textbuf, &(account_type_record->app_id));
5161 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SERVICE_PROVIDER_ID);
5162 _account_db_data_to_text(textbuf, &(account_type_record->service_provider_id));
5164 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_ICON_PATH);
5165 _account_db_data_to_text(textbuf, &(account_type_record->icon_path));
5167 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SMALL_ICON_PATH);
5168 _account_db_data_to_text(textbuf, &(account_type_record->small_icon_path));
5170 account_type_record->multiple_account_support = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_MULTIPLE_ACCOUNT_SUPPORT);
5174 static void _account_type_convert_column_to_label(account_stmt hstmt, label_s *label_record)
5176 const char *textbuf = NULL;
5178 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_APP_ID);
5179 _account_db_data_to_text(textbuf, &(label_record->app_id));
5181 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LABEL);
5182 _account_db_data_to_text(textbuf, &(label_record->label));
5184 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LOCALE);
5185 _account_db_data_to_text(textbuf, &(label_record->locale));
5189 GSList* _account_type_get_label_list_by_app_id(const char* app_id, int *error_code )
5191 *error_code = ACCOUNT_ERROR_NONE;
5192 account_stmt hstmt = NULL;
5193 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5194 int rc = 0, binding_count = 1;
5195 GSList* label_list = NULL;
5197 ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
5198 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
5200 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5202 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
5203 hstmt = _account_prepare_query(query);
5205 if( _account_db_err_code() == SQLITE_PERM ){
5206 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5207 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
5211 _account_query_bind_text(hstmt, binding_count++, app_id);
5213 rc = _account_query_step(hstmt);
5214 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5216 label_s* label_record = NULL;
5218 while (rc == SQLITE_ROW) {
5219 label_record = (label_s*) malloc(sizeof(label_s));
5221 if (label_record == NULL) {
5222 ACCOUNT_FATAL("malloc Failed");
5226 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
5228 _account_type_convert_column_to_label(hstmt, label_record);
5230 _INFO("Adding account label_list");
5231 label_list = g_slist_append (label_list, label_record);
5233 rc = _account_query_step(hstmt);
5236 rc = _account_query_finalize(hstmt);
5237 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
5240 *error_code = ACCOUNT_ERROR_NONE;
5243 if (hstmt != NULL) {
5244 rc = _account_query_finalize(hstmt);
5245 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
5249 pthread_mutex_unlock(&account_mutex);
5250 _INFO("Returning account label_list");
5254 int account_type_query_label_by_app_id(account_label_cb callback, const char* app_id, void *user_data )
5256 int error_code = ACCOUNT_ERROR_NONE;
5257 account_stmt hstmt = NULL;
5258 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5259 int rc = 0, binding_count = 1;
5261 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5262 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
5263 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5265 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5267 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
5268 hstmt = _account_prepare_query(query);
5270 if( _account_db_err_code() == SQLITE_PERM ){
5271 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5272 return ACCOUNT_ERROR_PERMISSION_DENIED;
5275 _account_query_bind_text(hstmt, binding_count++, app_id);
5277 rc = _account_query_step(hstmt);
5278 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5280 label_s* label_record = NULL;
5282 while (rc == SQLITE_ROW) {
5283 bool cb_ret = FALSE;
5284 label_record = (label_s*) malloc(sizeof(label_s));
5286 if (label_record == NULL) {
5287 ACCOUNT_FATAL("malloc Failed");
5291 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
5293 _account_type_convert_column_to_label(hstmt, label_record);
5295 cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data);
5297 _account_type_free_label_items(label_record);
5298 _ACCOUNT_FREE(label_record);
5300 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
5302 rc = _account_query_step(hstmt);
5305 rc = _account_query_finalize(hstmt);
5306 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5309 error_code = ACCOUNT_ERROR_NONE;
5312 if (hstmt != NULL) {
5313 rc = _account_query_finalize(hstmt);
5314 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5318 pthread_mutex_unlock(&account_mutex);
5322 int _account_type_label_get_app_id(label_h label, char **app_id)
5325 return ACCOUNT_ERROR_INVALID_PARAMETER;
5329 return ACCOUNT_ERROR_INVALID_PARAMETER;
5332 label_s *data = (label_s*)label;
5336 *app_id = _account_get_text(data->app_id);
5338 return ACCOUNT_ERROR_NONE;
5341 bool _account_get_label_text_cb(char* app_id, char* label, char* locale, void *user_data)
5343 account_type_s *data = (account_type_s*)user_data;
5345 label_s *label_data = (label_s*)malloc(sizeof(label_s));
5347 if (label_data == NULL) {
5348 ACCOUNT_FATAL("_account_get_label_text_cb : MALLOC FAIL\n");
5351 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
5353 label_data->app_id = _account_get_text(app_id);
5354 label_data->label = _account_get_text(label);
5355 label_data->locale = _account_get_text(locale);
5357 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
5362 bool _account_get_provider_feature_cb(char* app_id, char* key, void* user_data)
5364 account_type_s *data = (account_type_s*)user_data;
5366 provider_feature_s *feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
5368 if (feature_data == NULL) {
5369 ACCOUNT_FATAL("_account_get_provider_feature_cb : MALLOC FAIL\n");
5372 ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
5374 feature_data->app_id = _account_get_text(app_id);
5375 feature_data->key = _account_get_text(key);
5377 data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
5382 int _account_type_query_by_app_id(const char* app_id, account_type_s** account_type_record)
5384 _INFO("_account_type_query_by_app_id start");
5386 int error_code = ACCOUNT_ERROR_NONE;
5387 account_stmt hstmt = NULL;
5388 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5389 int rc = 0, binding_count = 1;
5391 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5392 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5394 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5396 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
5397 hstmt = _account_prepare_query(query);
5399 if( _account_db_err_code() == SQLITE_PERM ){
5400 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5401 return ACCOUNT_ERROR_PERMISSION_DENIED;
5404 _account_query_bind_text(hstmt, binding_count++, app_id);
5406 rc = _account_query_step(hstmt);
5407 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5409 *account_type_record = create_empty_account_type_instance();
5411 while (rc == SQLITE_ROW) {
5412 _account_type_convert_column_to_account_type(hstmt, *account_type_record);
5413 rc = _account_query_step(hstmt);
5416 rc = _account_query_finalize(hstmt);
5417 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5418 account_type_query_label_by_app_id(_account_get_label_text_cb, app_id, (void*)(*account_type_record));
5419 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, app_id,(void*)(*account_type_record));
5422 error_code = ACCOUNT_ERROR_NONE;
5425 if (hstmt != NULL) {
5426 rc = _account_query_finalize(hstmt);
5427 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5431 pthread_mutex_unlock(&account_mutex);
5432 _INFO("_account_type_query_by_app_id end [%d]", error_code);
5436 int _account_type_query_app_id_exist(const char* app_id)
5438 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5441 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5442 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5444 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5446 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
5447 rc = _account_get_record_count(query);
5449 if( _account_db_err_code() == SQLITE_PERM ){
5450 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5451 return ACCOUNT_ERROR_PERMISSION_DENIED;
5455 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5458 return ACCOUNT_ERROR_NONE;
5461 GSList* _account_type_query_by_provider_feature(const char* key, int *error_code)
5463 *error_code = ACCOUNT_ERROR_NONE;
5464 account_stmt hstmt = NULL;
5465 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5467 GSList *account_type_list = NULL;
5471 ACCOUNT_ERROR("capability_type IS NULL.");
5472 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
5476 if(g_hAccountDB == NULL)
5478 ACCOUNT_ERROR("The database isn't connected.");
5479 *error_code = ACCOUNT_ERROR_DB_NOT_OPENED;
5483 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5485 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
5487 hstmt = _account_prepare_query(query);
5489 if( _account_db_err_code() == SQLITE_PERM )
5491 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5492 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
5496 int binding_count = 1;
5497 _account_query_bind_text(hstmt, binding_count++, key);
5499 rc = _account_query_step(hstmt);
5501 account_type_s *account_type_record = NULL;
5503 if(rc != SQLITE_ROW)
5505 ACCOUNT_ERROR("The record isn't found.");
5506 *error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
5510 while(rc == SQLITE_ROW) {
5511 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
5513 if (account_type_record == NULL) {
5514 ACCOUNT_FATAL("malloc Failed");
5518 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
5519 _account_type_convert_column_to_account_type(hstmt, account_type_record);
5520 account_type_list = g_slist_append(account_type_list, account_type_record);
5521 rc = _account_query_step(hstmt);
5524 rc = _account_query_finalize(hstmt);
5525 if (rc != ACCOUNT_ERROR_NONE )
5527 _account_type_gslist_free(account_type_list);
5528 ACCOUNT_ERROR("finalize error(%s)", rc);
5536 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
5537 account_type_s *account_type = NULL;
5538 account_type = (account_type_s*)iter->data;
5539 account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
5540 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
5543 *error_code = ACCOUNT_ERROR_NONE;
5546 if (hstmt != NULL) {
5547 rc = _account_query_finalize(hstmt);
5548 if (rc != ACCOUNT_ERROR_NONE)
5556 return account_type_list;
5560 GSList* _account_type_query_all(void)
5562 account_stmt hstmt = NULL;
5563 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5565 GSList *account_type_list = NULL;
5567 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
5569 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5571 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
5572 hstmt = _account_prepare_query(query);
5574 rc = _account_query_step(hstmt);
5576 if( _account_db_err_code() == SQLITE_PERM ){
5577 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5581 account_type_s *account_type_record = NULL;
5583 if (rc != SQLITE_ROW)
5585 _INFO("[ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found.");
5589 while(rc == SQLITE_ROW) {
5590 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
5592 if (account_type_record == NULL) {
5593 ACCOUNT_FATAL("malloc Failed");
5597 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
5598 _account_type_convert_column_to_account_type(hstmt, account_type_record);
5599 account_type_list = g_slist_append(account_type_list, account_type_record);
5600 rc = _account_query_step(hstmt);
5603 rc = _account_query_finalize(hstmt);
5604 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, NULL, ("finalize error"));
5609 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
5610 account_type_s *account_type = NULL;
5611 account_type = (account_type_s*)iter->data;
5612 account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
5613 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
5619 rc = _account_query_finalize(hstmt);
5620 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {_account_type_gslist_free(account_type_list);}, NULL, ("finalize error"));
5624 return account_type_list;
5627 // output parameter label must be free
5628 int _account_type_query_label_by_locale(const char* app_id, const char* locale, char **label)
5630 int error_code = ACCOUNT_ERROR_NONE;
5631 account_stmt hstmt = NULL;
5632 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5633 int rc = 0, binding_count = 1;
5634 char* converted_locale = NULL;
5636 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
5637 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5638 ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
5639 ACCOUNT_RETURN_VAL((locale != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null"));
5640 //Making label newly created
5642 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5644 converted_locale = _account_get_text(locale);
5645 gchar** tokens = g_strsplit(converted_locale, "-", 2);
5647 if(tokens != NULL) {
5648 if((char*)(tokens[1]) != NULL) {
5649 char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
5650 if(upper_token != NULL) {
5651 _ACCOUNT_FREE(converted_locale);
5652 converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
5654 _ACCOUNT_FREE(upper_token);
5659 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale);
5660 _ACCOUNT_FREE(converted_locale);
5662 hstmt = _account_prepare_query(query);
5664 if( _account_db_err_code() == SQLITE_PERM ){
5665 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5666 return ACCOUNT_ERROR_PERMISSION_DENIED;
5669 _account_query_bind_text(hstmt, binding_count++, app_id);
5671 rc = _account_query_step(hstmt);
5672 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5674 label_s* label_record = NULL;
5676 while (rc == SQLITE_ROW) {
5677 label_record = (label_s*) malloc(sizeof(label_s));
5679 if (label_record == NULL) {
5680 ACCOUNT_FATAL("malloc Failed");
5684 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
5686 _account_type_convert_column_to_label(hstmt,label_record);
5688 _ACCOUNT_FREE(*label);
5689 //Making label newly created
5690 *label = _account_get_text(label_record->label);
5692 _account_type_free_label_items(label_record);
5693 _ACCOUNT_FREE(label_record);
5695 rc = _account_query_step(hstmt);
5698 rc = _account_query_finalize(hstmt);
5699 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5702 error_code = ACCOUNT_ERROR_NONE;
5705 if (hstmt != NULL) {
5706 rc = _account_query_finalize(hstmt);
5707 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5711 pthread_mutex_unlock(&account_mutex);
5712 _INFO("_account_type_query_label_by_locale() end : error_code = %d", error_code);
5716 static int _account_insert_custom(account_s *account, int account_id)
5718 _INFO("_account_insert_custom start");
5721 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5722 account_stmt hstmt = NULL;
5724 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
5726 if (g_slist_length( account->custom_list)==0) {
5727 ACCOUNT_DEBUG( "_account_insert_custom, no custom data\n");
5728 return ACCOUNT_ERROR_NONE;
5731 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
5733 rc = _account_get_record_count(query);
5735 if( _account_db_err_code() == SQLITE_PERM ){
5736 ACCOUNT_ERROR( "Access failed(%d, %s)", _account_db_err_msg());
5737 return ACCOUNT_ERROR_PERMISSION_DENIED;
5741 ACCOUNT_SLOGE( "_account_insert_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
5742 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5749 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
5752 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5753 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (AccountId, AppId, Key, Value) VALUES "
5754 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
5756 hstmt = _account_prepare_query(query);
5758 if( _account_db_err_code() == SQLITE_PERM ){
5759 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5760 return ACCOUNT_ERROR_PERMISSION_DENIED;
5763 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5765 account_custom_s* custom_data = NULL;
5766 custom_data = (account_custom_s*)iter->data;
5768 ret = _account_query_bind_int(hstmt, count++, account_id);
5769 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
5770 ret = _account_query_bind_text(hstmt, count++, account->package_name);
5771 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5772 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
5773 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5774 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
5775 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5777 rc = _account_query_step(hstmt);
5779 if (rc != SQLITE_DONE) {
5780 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5784 rc = _account_query_finalize(hstmt);
5785 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5790 _INFO("_account_insert_custom end");
5791 return ACCOUNT_ERROR_NONE;
5794 static int _account_update_custom(account_s *account, int account_id)
5797 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5798 account_stmt hstmt = NULL;
5800 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
5802 if (g_slist_length( account->custom_list)==0) {
5803 ACCOUNT_DEBUG( "_account_update_custom, no custom data\n");
5804 return ACCOUNT_ERROR_NONE;
5807 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
5809 rc = _account_get_record_count(query);
5811 if( _account_db_err_code() == SQLITE_PERM ){
5812 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5813 pthread_mutex_unlock(&account_mutex);
5814 return ACCOUNT_ERROR_PERMISSION_DENIED;
5815 } else if( _account_db_err_code() == SQLITE_BUSY ){
5816 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
5817 pthread_mutex_unlock(&account_mutex);
5818 return ACCOUNT_ERROR_DATABASE_BUSY;
5822 ACCOUNT_SLOGE( "_account_update_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
5823 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5826 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5828 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId=? ", ACCOUNT_CUSTOM_TABLE);
5829 hstmt = _account_prepare_query(query);
5831 _account_query_bind_int(hstmt, count++, (int)account_id);
5832 rc = _account_query_step(hstmt);
5834 if (rc == SQLITE_BUSY) {
5835 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5836 return ACCOUNT_ERROR_DATABASE_BUSY;
5837 } else if (rc != SQLITE_DONE) {
5838 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5839 return ACCOUNT_ERROR_DB_FAILED;
5842 rc = _account_query_finalize(hstmt);
5843 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5848 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
5851 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5852 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AccountId, AppId, Key, Value) VALUES "
5853 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
5855 hstmt = _account_prepare_query(query);
5857 if( _account_db_err_code() == SQLITE_PERM ){
5858 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5859 return ACCOUNT_ERROR_PERMISSION_DENIED;
5862 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5864 account_custom_s* custom_data = NULL;
5865 custom_data = (account_custom_s*)iter->data;
5867 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
5868 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
5869 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
5870 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5871 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
5872 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5873 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
5874 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5876 rc = _account_query_step(hstmt);
5878 if (rc != SQLITE_DONE) {
5879 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5883 rc = _account_query_finalize(hstmt);
5884 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5889 return ACCOUNT_ERROR_NONE;
5892 static int _account_query_custom_by_account_id(account_custom_cb callback, int account_id, void *user_data )
5894 int error_code = ACCOUNT_ERROR_NONE;
5895 account_stmt hstmt = NULL;
5896 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5899 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
5900 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
5901 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5903 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5905 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
5906 hstmt = _account_prepare_query(query);
5908 if( _account_db_err_code() == SQLITE_PERM ){
5909 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5910 return ACCOUNT_ERROR_PERMISSION_DENIED;
5913 rc = _account_query_step(hstmt);
5915 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5917 account_custom_s* custom_record = NULL;
5919 while (rc == SQLITE_ROW) {
5920 bool cb_ret = FALSE;
5921 custom_record = (account_custom_s*) malloc(sizeof(account_custom_s));
5923 if (custom_record == NULL) {
5924 ACCOUNT_FATAL("malloc Failed");
5928 ACCOUNT_MEMSET(custom_record, 0x00, sizeof(account_custom_s));
5930 _account_convert_column_to_custom(hstmt, custom_record);
5932 cb_ret = callback(custom_record->key, custom_record->value, user_data);
5934 _account_custom_item_free(custom_record);
5935 _ACCOUNT_FREE(custom_record);
5937 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
5939 rc = _account_query_step(hstmt);
5942 rc = _account_query_finalize(hstmt);
5943 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5946 error_code = ACCOUNT_ERROR_NONE;
5949 if (hstmt != NULL) {
5950 rc = _account_query_finalize(hstmt);
5951 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5955 pthread_mutex_unlock(&account_mutex);