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;
1836 *account_id = _account_get_next_sequence(ACCOUNT_TABLE);
1837 data->id = *account_id;
1840 appid = _account_get_current_appid(pid);
1846 // API caller cannot be recognized
1847 ret_transaction = _account_end_transaction(FALSE);
1848 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1849 pthread_mutex_unlock(&account_mutex);
1850 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
1854 char* verified_appid = NULL;
1855 error_code = _account_check_account_type_with_appid_group(appid, &verified_appid);//FIX
1856 _ACCOUNT_FREE(appid);
1857 if(error_code != ACCOUNT_ERROR_NONE)
1859 _ERR("error_code = %d", error_code);
1860 ret_transaction = _account_end_transaction(FALSE);
1861 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
1862 _ACCOUNT_FREE(verified_appid);
1863 pthread_mutex_unlock(&account_mutex);
1870 error_code = _account_check_duplicated(data, verified_appid);
1871 if (error_code != ACCOUNT_ERROR_NONE) {
1873 ret_transaction = _account_end_transaction(FALSE);
1874 ACCOUNT_DEBUG("_account_check_duplicated(), rollback insert query(%x)!!!!\n", ret_transaction);
1876 pthread_mutex_unlock(&account_mutex);
1879 if(!_account_check_add_more_account(verified_appid)) {
1880 ret_transaction = _account_end_transaction(FALSE);
1881 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
1882 pthread_mutex_unlock(&account_mutex);
1883 _ACCOUNT_FREE(verified_appid);
1884 return ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
1887 _ACCOUNT_FREE(data->package_name);
1888 data->package_name = _account_get_text(verified_appid);
1889 _ACCOUNT_FREE(verified_appid);
1892 if(!_account_check_add_more_account(data->package_name))
1895 ret_transaction = _account_end_transaction(FALSE);
1896 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
1897 pthread_mutex_unlock(&account_mutex);
1898 return ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
1901 error_code = _account_execute_insert_query(data);
1903 if (error_code != ACCOUNT_ERROR_NONE)
1906 ret_transaction = _account_end_transaction(FALSE);
1907 ACCOUNT_ERROR("INSERT account fail, rollback insert query(%x)!!!!\n", ret_transaction);
1909 pthread_mutex_unlock(&account_mutex);
1914 error_code = _account_insert_capability(data, *account_id);
1915 if (error_code != ACCOUNT_ERROR_NONE)
1918 ret_transaction = _account_end_transaction(FALSE);
1919 ACCOUNT_ERROR("INSERT capability fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1921 pthread_mutex_unlock(&account_mutex);
1926 error_code = _account_insert_custom(data, *account_id);
1927 if (error_code != ACCOUNT_ERROR_NONE)
1929 ret_transaction = _account_end_transaction(FALSE);
1930 ACCOUNT_ERROR("INSERT custom fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
1932 pthread_mutex_unlock(&account_mutex);
1938 pthread_mutex_unlock(&account_mutex);
1939 _account_end_transaction(TRUE);
1940 ACCOUNT_SLOGD("(%s)-(%d) account _end_transaction.\n", __FUNCTION__, __LINE__);
1943 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_INSERT, *account_id);
1944 _account_insert_delete_update_notification_send(buf);
1945 _INFO("account _notification_send end.");
1947 return ACCOUNT_ERROR_NONE;
1951 int _account_query_capability_by_account_id(capability_cb callback, int account_id, void *user_data )
1953 int error_code = ACCOUNT_ERROR_NONE;
1954 account_stmt hstmt = NULL;
1955 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1958 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1959 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
1960 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1962 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1964 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
1965 hstmt = _account_prepare_query(query);
1967 if( _account_db_err_code() == SQLITE_PERM ){
1968 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1969 return ACCOUNT_ERROR_PERMISSION_DENIED;
1972 rc = _account_query_step(hstmt);
1973 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1975 account_capability_s* capability_record = NULL;
1977 while (rc == SQLITE_ROW) {
1978 bool cb_ret = FALSE;
1979 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
1981 if (capability_record == NULL) {
1982 ACCOUNT_FATAL("malloc Failed");
1986 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
1988 _account_convert_column_to_capability(hstmt, capability_record);
1990 cb_ret = callback(capability_record->type, capability_record->value, user_data);
1992 _account_free_capability_items(capability_record);
1993 _ACCOUNT_FREE(capability_record);
1995 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
1997 rc = _account_query_step(hstmt);
2000 rc = _account_query_finalize(hstmt);
2001 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2004 error_code = ACCOUNT_ERROR_NONE;
2007 if (hstmt != NULL) {
2008 rc = _account_query_finalize(hstmt);
2009 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2013 pthread_mutex_unlock(&account_mutex);
2017 GSList* _account_get_capability_list_by_account_id(int account_id, int *error_code)
2019 *error_code = ACCOUNT_ERROR_NONE;
2020 account_stmt hstmt = NULL;
2021 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2023 GSList* capability_list = NULL;
2025 ACCOUNT_RETURN_VAL((account_id > 0), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("ACCOUNT INDEX IS LESS THAN 0"));
2026 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
2028 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2030 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2031 hstmt = _account_prepare_query(query);
2033 if( _account_db_err_code() == SQLITE_PERM ){
2034 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2035 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
2039 rc = _account_query_step(hstmt);
2040 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2042 account_capability_s* capability_record = NULL;
2044 while (rc == SQLITE_ROW) {
2045 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
2047 if (capability_record == NULL) {
2048 ACCOUNT_FATAL("malloc Failed");
2052 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
2054 _account_convert_column_to_capability(hstmt, capability_record);
2056 //cb_ret = callback(capability_record->type, capability_record->value, user_data);
2058 //_account_free_capability_items(capability_record);
2059 //_ACCOUNT_FREE(capability_record);
2061 //ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
2063 capability_list = g_slist_append(capability_list, capability_record);
2064 rc = _account_query_step(hstmt);
2067 rc = _account_query_finalize(hstmt);
2068 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
2071 *error_code = ACCOUNT_ERROR_NONE;
2076 rc = _account_query_finalize(hstmt);
2077 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
2081 pthread_mutex_unlock(&account_mutex);
2082 return capability_list;
2085 static int _account_compare_old_record(account_s *new_account, int account_id)
2087 int error_code = ACCOUNT_ERROR_NONE;
2088 account_stmt hstmt = NULL;
2089 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2091 account_s *old_account = NULL;
2093 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2094 ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2095 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2097 old_account = (account_s*)calloc(1, sizeof(account_s));
2099 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2101 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2102 hstmt = _account_prepare_query(query);
2104 rc = _account_query_step(hstmt);
2105 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2107 while (rc == SQLITE_ROW) {
2108 _account_convert_column_to_account(hstmt, old_account);
2109 rc = _account_query_step(hstmt);
2112 rc = _account_query_finalize(hstmt);
2113 ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2117 error_code = _account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account);
2118 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
2121 error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account);
2122 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
2126 new_account->id = old_account->id;
2129 if(!new_account->user_name) {
2130 if(old_account->user_name)
2131 new_account->user_name = _account_get_text(old_account->user_name);
2135 if(!new_account->display_name) {
2136 if(old_account->display_name)
2137 new_account->display_name = _account_get_text(old_account->display_name);
2141 if(!new_account->email_address) {
2142 if(old_account->email_address)
2143 new_account->email_address = _account_get_text(old_account->email_address);
2147 if(!new_account->domain_name) {
2148 if(old_account->domain_name)
2149 new_account->domain_name = _account_get_text(old_account->domain_name);
2153 if(!new_account->icon_path) {
2154 if(old_account->icon_path)
2155 new_account->icon_path = _account_get_text(old_account->icon_path);
2159 if(!new_account->source) {
2160 if(old_account->source)
2161 new_account->source = _account_get_text(old_account->source);
2164 _ACCOUNT_FREE(new_account->package_name);
2165 new_account->package_name = _account_get_text(old_account->package_name);
2168 if(!new_account->access_token) {
2169 if(old_account->access_token)
2170 new_account->access_token = _account_get_text(old_account->access_token);
2175 for(i=0;i<USER_TXT_CNT;i++) {
2176 if(!new_account->user_data_txt[i]) {
2177 if(old_account->user_data_txt[i])
2178 new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]);
2183 if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) {
2184 new_account->auth_type = old_account->auth_type;
2188 if(new_account->secret== ACCOUNT_SECRECY_INVALID) {
2189 new_account->secret = old_account->secret;
2193 if(new_account->sync_support == ACCOUNT_SYNC_INVALID) {
2194 new_account->sync_support = old_account->sync_support;
2198 for(i=0;i<USER_INT_CNT;i++) {
2199 if(new_account->user_data_int[i] == 0) {
2200 new_account->user_data_int[i] = old_account->user_data_int[i];
2206 // user custom table
2210 _account_free_account_items(old_account);
2211 _ACCOUNT_FREE(old_account);
2214 if (hstmt != NULL) {
2215 rc = _account_query_finalize(hstmt);
2216 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2220 return ACCOUNT_ERROR_NONE;
2223 static int _account_get_package_name_from_account_id(int account_id, char **package_name)
2225 int error_code = ACCOUNT_ERROR_NONE;
2226 account_stmt hstmt = NULL;
2227 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2229 account_s *old_account = NULL;
2231 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2232 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2234 old_account = (account_s*)calloc(1, sizeof(account_s));
2236 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2238 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2239 hstmt = _account_prepare_query(query);
2241 rc = _account_query_step(hstmt);
2242 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2244 while (rc == SQLITE_ROW) {
2245 _account_convert_column_to_account(hstmt, old_account);
2246 rc = _account_query_step(hstmt);
2249 rc = _account_query_finalize(hstmt);
2250 ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2253 // get package name.
2254 *package_name = _account_get_text(old_account->package_name);
2259 _account_free_account_items(old_account);
2260 _ACCOUNT_FREE(old_account);
2263 if (hstmt != NULL) {
2264 rc = _account_query_finalize(hstmt);
2265 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2273 static int _account_update_account(int pid, account_s *account, int account_id)
2275 int rc = 0, binding_count =0;
2276 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2277 int error_code = ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0;
2278 account_stmt hstmt = NULL;
2280 if (!account->package_name) {
2281 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
2282 return ACCOUNT_ERROR_INVALID_PARAMETER;
2285 /* Check permission of requested appid */
2286 char* current_appid = NULL;
2287 char *package_name = NULL;
2289 current_appid = _account_get_current_appid(pid);
2290 error_code = _account_get_package_name_from_account_id(account_id, &package_name);
2292 if(error_code != ACCOUNT_ERROR_NONE || package_name == NULL){
2293 ACCOUNT_ERROR("No package name with account_id\n");
2294 _ACCOUNT_FREE(current_appid);
2295 _ACCOUNT_FREE(package_name);
2296 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2299 error_code = _account_check_appid_group_with_package_name(current_appid, package_name);
2300 ACCOUNT_DEBUG( "UPDATE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name); // TODO: remove the log later.
2302 _ACCOUNT_FREE(current_appid);
2303 _ACCOUNT_FREE(package_name);
2305 if(error_code != ACCOUNT_ERROR_NONE){
2306 ACCOUNT_ERROR("No permission to update\n");
2307 return ACCOUNT_ERROR_PERMISSION_DENIED;
2310 _account_compare_old_record(account, account_id);
2312 if( _account_db_err_code() == SQLITE_PERM ){
2313 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2314 return ACCOUNT_ERROR_PERMISSION_DENIED;
2315 } else if( _account_db_err_code() == SQLITE_BUSY ){
2316 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
2317 return ACCOUNT_ERROR_DATABASE_BUSY;
2320 if (!account->user_name && !account->display_name && !account->email_address) {
2321 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
2322 return ACCOUNT_ERROR_INVALID_PARAMETER;
2325 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2327 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
2329 count = _account_get_record_count(query);
2331 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
2332 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2335 /* transaction control required*/
2336 ret_transaction = _account_begin_transaction();
2337 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
2338 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
2339 pthread_mutex_unlock(&account_mutex);
2340 return ACCOUNT_ERROR_DATABASE_BUSY;
2343 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2344 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
2345 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
2346 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
2347 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
2349 hstmt = _account_prepare_query(query);
2351 if( _account_db_err_code() == SQLITE_PERM ){
2352 ret_transaction = _account_end_transaction(FALSE);
2353 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2354 return ACCOUNT_ERROR_PERMISSION_DENIED;
2357 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)));
2359 binding_count = _account_convert_account_to_sql(account, hstmt, query);
2360 _account_query_bind_int(hstmt, binding_count++, account_id);
2362 rc = _account_query_step(hstmt);
2363 if (rc != SQLITE_DONE) {
2364 ACCOUNT_SLOGE( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2367 rc = _account_query_finalize(hstmt);
2368 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2371 _INFO("update query=%s", query);
2373 /*update capability*/
2374 error_code = _account_update_capability(account, account_id);
2375 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2376 ret_transaction = _account_end_transaction(FALSE);
2377 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2382 error_code = _account_update_custom(account, account_id);
2383 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2384 ret_transaction = _account_end_transaction(FALSE);
2385 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2389 ret_transaction = _account_end_transaction(TRUE);
2391 _INFO("update end");
2396 static int _account_update_account_ex(account_s *account, int account_id)
2398 int rc = 0, binding_count =0;
2399 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2400 int error_code = ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0;
2401 account_stmt hstmt = NULL;
2403 if (!account->package_name) {
2404 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
2405 return ACCOUNT_ERROR_INVALID_PARAMETER;
2408 _account_compare_old_record(account, account_id);
2410 if( _account_db_err_code() == SQLITE_PERM ){
2411 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2412 return ACCOUNT_ERROR_PERMISSION_DENIED;
2415 if (!account->user_name && !account->display_name && !account->email_address) {
2416 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
2417 return ACCOUNT_ERROR_INVALID_PARAMETER;
2420 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2422 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
2424 count = _account_get_record_count(query);
2426 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
2427 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2430 /* transaction control required*/
2431 ret_transaction = _account_begin_transaction();
2432 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
2433 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
2434 pthread_mutex_unlock(&account_mutex);
2435 return ACCOUNT_ERROR_DATABASE_BUSY;
2438 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2439 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
2440 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
2441 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
2442 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
2444 hstmt = _account_prepare_query(query);
2446 if( _account_db_err_code() == SQLITE_PERM ){
2447 ret_transaction = _account_end_transaction(FALSE);
2448 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2449 return ACCOUNT_ERROR_PERMISSION_DENIED;
2452 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)));
2454 _INFO("account_update_to_db_by_id_ex_p : before convert() : account_id[%d], user_name=%s", account->id, account->user_name);
2455 binding_count = _account_convert_account_to_sql(account, hstmt, query);
2456 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], user_name=%s", account->id, account->user_name);
2457 _INFO("account_update_to_db_by_id_ex_p : before bind()");
2458 rc = _account_query_bind_int(hstmt, binding_count++, account_id);
2459 _INFO("account_update_to_db_by_id_ex_p : after bind() : ret = %d", rc);
2461 rc = _account_query_step(hstmt);
2462 if (rc != SQLITE_DONE) {
2463 ACCOUNT_SLOGE( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2465 _INFO("account_update_to_db_by_id_ex_p : after query_step() : ret = %d", rc);
2467 rc = _account_query_finalize(hstmt);
2468 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2470 _INFO("account_update_to_db_by_id_ex_p : after query_filnalize() : ret = %d", rc);
2472 _INFO("account_update_to_db_by_id_ex_p : before update_capability()");
2473 /*update capability*/
2474 error_code = _account_update_capability(account, account_id);
2475 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2476 ret_transaction = _account_end_transaction(FALSE);
2477 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2480 _INFO("account_update_to_db_by_id_ex_p : after update_capability()");
2482 _INFO("account_update_to_db_by_id_ex_p : before update_custom()");
2484 error_code = _account_update_custom(account, account_id);
2485 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2486 ret_transaction = _account_end_transaction(FALSE);
2487 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2490 _INFO("account_update_to_db_by_id_ex_p : after update_custom()");
2492 ret_transaction = _account_end_transaction(TRUE);
2498 int _account_update_to_db_by_id(int pid, account_s* account, int account_id)
2500 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
2501 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
2502 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2503 int error_code = ACCOUNT_ERROR_NONE;
2504 account_s* data = (account_s*)account;
2506 pthread_mutex_lock(&account_mutex);
2508 error_code = _account_update_account(pid, data, account_id);
2510 if(error_code != ACCOUNT_ERROR_NONE) {
2511 pthread_mutex_unlock(&account_mutex);
2515 pthread_mutex_unlock(&account_mutex);
2518 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, account_id);
2519 _account_insert_delete_update_notification_send(buf);
2521 return ACCOUNT_ERROR_NONE;
2524 int _account_update_to_db_by_id_ex(account_s* account, int account_id)
2526 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
2527 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
2528 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2529 int error_code = ACCOUNT_ERROR_NONE;
2530 account_s* data = account;
2532 pthread_mutex_lock(&account_mutex);
2534 _INFO("before update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
2535 error_code = _account_update_account_ex(data, account_id);
2536 _INFO("after update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
2538 if(error_code != ACCOUNT_ERROR_NONE) {
2539 pthread_mutex_unlock(&account_mutex);
2543 pthread_mutex_unlock(&account_mutex);
2546 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, account_id);
2547 _account_insert_delete_update_notification_send(buf);
2549 return ACCOUNT_ERROR_NONE;
2553 int _account_update_to_db_by_user_name(int pid, account_s* account, const char *user_name, const char *package_name)
2555 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
2556 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
2557 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2559 int error_code = ACCOUNT_ERROR_NONE;
2560 account_s *data = (account_s*)account;
2562 pthread_mutex_lock(&account_mutex);
2564 error_code = _account_update_account_by_user_name(pid, data, user_name, package_name);
2566 pthread_mutex_unlock(&account_mutex);
2569 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, data->id);
2570 _account_insert_delete_update_notification_send(buf);
2575 GSList* _account_db_query_all(int pid)
2577 //int error_code = ACCOUNT_ERROR_NONE;
2578 account_stmt hstmt = NULL;
2579 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2581 GSList *account_list = NULL;
2583 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
2585 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2587 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TABLE);
2588 hstmt = _account_prepare_query(query);
2590 if( _account_db_err_code() == SQLITE_PERM ){
2591 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2595 rc = _account_query_step(hstmt);
2597 account_s *account_record = NULL;
2599 if (rc != SQLITE_ROW)
2601 _ERR("The record isn't found");
2605 while(rc == SQLITE_ROW) {
2606 account_record = (account_s*) malloc(sizeof(account_s));
2608 if (account_record == NULL) {
2609 ACCOUNT_FATAL("malloc Failed");
2613 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2614 _account_convert_column_to_account(hstmt, account_record);
2615 account_list = g_slist_append(account_list, account_record);
2616 rc = _account_query_step(hstmt);
2619 rc = _account_query_finalize(hstmt);
2620 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, NULL, ("finalize error"));
2625 for (iter = account_list; iter != NULL; iter = g_slist_next(iter)) {
2626 account_s *account = NULL;
2627 account = (account_s*)iter->data;
2628 _account_query_capability_by_account_id(_account_get_capability_text_cb, account->id, (void*)account);
2629 _account_query_custom_by_account_id(_account_get_custom_text_cb, account->id, (void*)account);
2633 if (hstmt != NULL) {
2634 rc = _account_query_finalize(hstmt);
2635 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {_account_gslist_free(account_list);}, NULL, ("finalize error"));
2640 _remove_sensitive_info_from_non_owning_account_slist(pid, account_list);
2642 return account_list;
2645 int _account_update_sync_status_by_id(int account_db_id, const int sync_status)
2647 int error_code = ACCOUNT_ERROR_NONE;
2648 account_stmt hstmt = NULL;
2649 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2653 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2654 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2655 if ( (sync_status < 0) || (sync_status > ACCOUNT_SYNC_STATUS_RUNNING)) {
2656 ACCOUNT_SLOGE("(%s)-(%d) sync_status is less than 0 or more than enum max.\n", __FUNCTION__, __LINE__);
2657 return ACCOUNT_ERROR_INVALID_PARAMETER;
2660 pthread_mutex_lock(&account_mutex);
2662 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2664 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_db_id);
2666 rc = _account_get_record_count(query);
2668 if( _account_db_err_code() == SQLITE_PERM ){
2669 pthread_mutex_unlock(&account_mutex);
2670 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2671 return ACCOUNT_ERROR_PERMISSION_DENIED;
2675 ACCOUNT_SLOGE( "account_update_sync_status_by_id : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
2676 pthread_mutex_unlock(&account_mutex);
2677 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2680 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2682 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET sync_support=? WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
2683 hstmt = _account_prepare_query(query);
2685 _account_query_bind_int(hstmt, count, sync_status);
2687 rc = _account_query_step(hstmt);
2689 if( _account_db_err_code() == SQLITE_PERM ){
2690 pthread_mutex_unlock(&account_mutex);
2691 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2692 return ACCOUNT_ERROR_PERMISSION_DENIED;
2695 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_DB_FAILED,
2696 ("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()));
2698 rc = _account_query_finalize(hstmt);
2699 if (rc != ACCOUNT_ERROR_NONE) {
2700 ACCOUNT_ERROR("_account_query_finalize error");
2701 pthread_mutex_unlock(&account_mutex);
2705 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_SYNC_UPDATE, account_db_id);
2706 _account_insert_delete_update_notification_send(buf);
2709 error_code = ACCOUNT_ERROR_NONE;
2712 if (hstmt != NULL) {
2713 rc = _account_query_finalize(hstmt);
2714 pthread_mutex_unlock(&account_mutex);
2715 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2719 pthread_mutex_unlock(&account_mutex);
2723 int _account_query_account_by_account_id(int pid, int account_db_id, account_s *account_record)
2725 _INFO("_account_query_account_by_account_id() start, account_db_id=[%d]", account_db_id);
2727 int error_code = ACCOUNT_ERROR_NONE;
2728 account_stmt hstmt = NULL;
2729 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2732 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2733 ACCOUNT_RETURN_VAL(account_record != NULL, {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2734 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2736 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2738 ACCOUNT_DEBUG("starting db operations");
2740 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
2741 hstmt = _account_prepare_query(query);
2742 rc = _account_db_err_code();
2743 _INFO("after _account_prepare_query, rc=[%d]", rc);
2745 if( rc == SQLITE_PERM ){
2746 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2747 return ACCOUNT_ERROR_PERMISSION_DENIED;
2750 ACCOUNT_DEBUG("before _account_query_step");
2751 rc = _account_query_step(hstmt);
2752 ACCOUNT_DEBUG("after _account_query_step returned [%d]", rc);
2753 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2755 while (rc == SQLITE_ROW) {
2756 ACCOUNT_DEBUG("before _account_convert_column_to_account");
2757 _account_convert_column_to_account(hstmt, account_record);
2758 ACCOUNT_DEBUG("after _account_convert_column_to_account");
2759 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]);
2760 rc = _account_query_step(hstmt);
2763 ACCOUNT_DEBUG("account_record->id=[%d]", account_record->id);
2765 rc = _account_query_finalize(hstmt);
2766 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2768 ACCOUNT_DEBUG("before _account_query_capability_by_account_id");
2769 _account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
2770 ACCOUNT_DEBUG("after _account_query_capability_by_account_id");
2772 ACCOUNT_DEBUG("before _account_query_custom_by_account_id");
2773 _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record);
2774 ACCOUNT_DEBUG("after _account_query_custom_by_account_id");
2777 error_code = ACCOUNT_ERROR_NONE;
2780 if (hstmt != NULL) {
2781 rc = _account_query_finalize(hstmt);
2782 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2788 _remove_sensitive_info_from_non_owning_account(pid, account_record);
2790 pthread_mutex_unlock(&account_mutex);
2791 ACCOUNT_DEBUG("_account_query_account_by_account_id end [%d]", error_code);
2795 GList* _account_query_account_by_user_name(int pid, const char *user_name, int *error_code)
2797 *error_code = ACCOUNT_ERROR_NONE;
2798 account_stmt hstmt = NULL;
2799 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2801 account_s *account_head = NULL;
2803 if (user_name == NULL)
2805 _ERR("USER NAME IS NULL");
2806 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
2810 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2812 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ?", ACCOUNT_TABLE);
2814 hstmt = _account_prepare_query(query);
2816 if (_account_db_err_code() == SQLITE_PERM)
2818 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2819 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
2823 int binding_count = 1;
2824 _account_query_bind_text(hstmt, binding_count++, user_name);
2826 rc = _account_query_step(hstmt);
2828 if (rc != SQLITE_ROW)
2830 _ERR("The record isn't found");
2831 *error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
2837 account_head = (account_s*) malloc(sizeof(account_s));
2838 if (account_head == NULL) {
2839 ACCOUNT_FATAL("malloc Failed");
2840 if (hstmt != NULL) {
2841 rc = _account_query_finalize(hstmt);
2843 if (rc != ACCOUNT_ERROR_NONE)
2845 _ERR("finalize error");
2851 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
2854 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2856 while (rc == SQLITE_ROW) {
2857 account_s* account_record = NULL;
2859 account_record = (account_s*) malloc(sizeof(account_s));
2861 if (account_record == NULL) {
2862 ACCOUNT_FATAL("malloc Failed");
2865 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2867 _account_convert_column_to_account(hstmt, account_record);
2869 account_head->account_list = g_list_append(account_head->account_list, account_record);
2871 rc = _account_query_step(hstmt);
2875 rc = _account_query_finalize(hstmt);
2877 if (rc != ACCOUNT_ERROR_NONE)
2879 _ERR("finalize error");
2889 tmp = g_list_length(account_head->account_list);
2891 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2893 account = (account_h)iter->data;
2895 account_s *testaccount = (account_s*)account;
2897 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
2898 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
2902 *error_code = ACCOUNT_ERROR_NONE;
2905 if (hstmt != NULL) {
2906 rc = _account_query_finalize(hstmt);
2907 if (rc != ACCOUNT_ERROR_NONE)
2909 _ERR("finalize error");
2915 pthread_mutex_unlock(&account_mutex);
2918 _remove_sensitive_info_from_non_owning_account_list(pid, account_head->account_list);
2919 GList* result = account_head->account_list;
2920 _ACCOUNT_FREE(account_head);
2927 _account_query_account_by_capability(int pid, const char* capability_type, const int capability_value, int *error_code)
2929 *error_code = ACCOUNT_ERROR_NONE;
2930 account_stmt hstmt = NULL;
2931 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2934 ACCOUNT_RETURN_VAL((capability_type != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("capability_type IS NULL"));
2936 if ((capability_value < 0) || (capability_value > ACCOUNT_CAPABILITY_ENABLED)) {
2937 ACCOUNT_SLOGE("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
2938 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
2942 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
2944 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2946 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=? AND value=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
2948 hstmt = _account_prepare_query(query);
2950 if( _account_db_err_code() == SQLITE_PERM ){
2951 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2952 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
2956 int binding_count = 1;
2957 _account_query_bind_text(hstmt, binding_count++, capability_type);
2958 _account_query_bind_int(hstmt, binding_count++, capability_value);
2960 rc = _account_query_step(hstmt);
2962 account_s* account_head = NULL;
2964 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2968 account_head = (account_s*) malloc(sizeof(account_s));
2969 if (account_head == NULL) {
2970 ACCOUNT_FATAL("malloc Failed");
2971 if (hstmt != NULL) {
2972 rc = _account_query_finalize(hstmt);
2973 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
2976 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
2979 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2981 while (rc == SQLITE_ROW) {
2982 account_s* account_record = NULL;
2984 account_record = (account_s*) malloc(sizeof(account_s));
2986 if (account_record == NULL) {
2987 ACCOUNT_FATAL("malloc Failed");
2990 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2992 _account_convert_column_to_account(hstmt, account_record);
2994 account_head->account_list = g_list_append(account_head->account_list, account_record);
2996 rc = _account_query_step(hstmt);
3000 rc = _account_query_finalize(hstmt);
3001 ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3007 tmp = g_list_length(account_head->account_list);
3009 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3010 account_h account = NULL;
3011 account = (account_h)iter->data;
3012 account_s* testaccount = (account_s*)account;
3014 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3015 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3020 *error_code = ACCOUNT_ERROR_NONE;
3023 if (hstmt != NULL) {
3024 rc = _account_query_finalize(hstmt);
3025 if ( rc != ACCOUNT_ERROR_NONE ) {
3027 _ERR("finalize error");
3032 if( *error_code != ACCOUNT_ERROR_NONE && account_head ) {
3033 _account_glist_free(account_head->account_list);
3034 _ACCOUNT_FREE(account_head);
3035 account_head = NULL;
3038 pthread_mutex_unlock(&account_mutex);
3042 _remove_sensitive_info_from_non_owning_account_list(pid, account_head->account_list);
3043 GList* result = account_head->account_list;
3044 _ACCOUNT_FREE(account_head);
3050 GList* _account_query_account_by_capability_type(int pid, const char* capability_type, int *error_code)
3052 *error_code = ACCOUNT_ERROR_NONE;
3053 account_stmt hstmt = NULL;
3054 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3057 ACCOUNT_RETURN_VAL((capability_type != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("capability_type IS NULL"));
3058 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;},
3059 NULL, ("The database isn't connected."));
3061 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3063 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
3065 hstmt = _account_prepare_query(query);
3067 if( _account_db_err_code() == SQLITE_PERM ){
3068 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3069 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
3073 int binding_count = 1;
3074 _account_query_bind_text(hstmt, binding_count++, capability_type);
3076 rc = _account_query_step(hstmt);
3078 account_s* account_head = NULL;
3080 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3084 account_head = (account_s*) malloc(sizeof(account_s));
3085 if (account_head == NULL) {
3086 ACCOUNT_FATAL("malloc Failed");
3087 if (hstmt != NULL) {
3088 rc = _account_query_finalize(hstmt);
3089 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
3092 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
3095 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3097 while (rc == SQLITE_ROW) {
3098 account_s* account_record = NULL;
3100 account_record = (account_s*) malloc(sizeof(account_s));
3102 if (account_record == NULL) {
3103 ACCOUNT_FATAL("malloc Failed");
3106 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3108 _account_convert_column_to_account(hstmt, account_record);
3110 account_head->account_list = g_list_append(account_head->account_list, account_record);
3112 rc = _account_query_step(hstmt);
3116 rc = _account_query_finalize(hstmt);
3117 ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3123 tmp = g_list_length(account_head->account_list);
3125 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3126 account_s* testaccount = (account_s*)iter->data;
3128 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3129 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3133 *error_code = ACCOUNT_ERROR_NONE;
3138 rc = _account_query_finalize(hstmt);
3139 if (rc != ACCOUNT_ERROR_NONE) {
3141 _ERR("finalize error");
3146 if( (*error_code != ACCOUNT_ERROR_NONE) && account_head ) {
3147 _account_glist_free(account_head->account_list);
3148 _ACCOUNT_FREE(account_head);
3149 account_head = NULL;
3152 pthread_mutex_unlock(&account_mutex);
3156 _remove_sensitive_info_from_non_owning_account_list(pid, account_head->account_list);
3157 GList* result = account_head->account_list;
3158 _ACCOUNT_FREE(account_head);
3164 GList* _account_query_account_by_package_name(int pid,const char* package_name, int *error_code)
3166 _INFO("_account_query_account_by_package_name");
3168 *error_code = ACCOUNT_ERROR_NONE;
3169 account_stmt hstmt = NULL;
3170 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3173 ACCOUNT_RETURN_VAL((package_name != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("PACKAGE NAME IS NULL"));
3174 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
3176 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3178 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE package_name=?", ACCOUNT_TABLE);
3180 hstmt = _account_prepare_query(query);
3182 if( _account_db_err_code() == SQLITE_PERM ){
3183 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3184 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
3188 int binding_count = 1;
3189 _account_query_bind_text(hstmt, binding_count++, package_name);
3191 rc = _account_query_step(hstmt);
3193 account_s* account_head = NULL;
3195 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.(%s)\n", package_name));
3199 account_head = (account_s*) malloc(sizeof(account_s));
3200 if (account_head == NULL) {
3201 ACCOUNT_FATAL("malloc Failed");
3202 if (hstmt != NULL) {
3203 rc = _account_query_finalize(hstmt);
3204 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
3207 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
3210 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3212 while (rc == SQLITE_ROW) {
3213 account_s* account_record = NULL;
3215 account_record = (account_s*) malloc(sizeof(account_s));
3217 if (account_record == NULL) {
3218 ACCOUNT_FATAL("malloc Failed");
3221 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3223 _account_convert_column_to_account(hstmt, account_record);
3225 _INFO("Adding account_list");
3226 account_head->account_list = g_list_append(account_head->account_list, account_record);
3228 rc = _account_query_step(hstmt);
3232 rc = _account_query_finalize(hstmt);
3233 ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3238 tmp = g_list_length(account_head->account_list);
3240 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3241 account_s* testaccount = (account_s*)iter->data;
3243 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3244 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3247 *error_code = ACCOUNT_ERROR_NONE;
3252 rc = _account_query_finalize(hstmt);
3253 if (rc != ACCOUNT_ERROR_NONE) {
3255 _ERR("finalize error");
3260 pthread_mutex_unlock(&account_mutex);
3262 if( (*error_code != ACCOUNT_ERROR_NONE) && account_head ) {
3263 _account_glist_free(account_head->account_list);
3264 _ACCOUNT_FREE(account_head);
3265 account_head = NULL;
3268 if ((*error_code == ACCOUNT_ERROR_NONE) && account_head != NULL)
3270 _INFO("Returning account_list");
3271 _remove_sensitive_info_from_non_owning_account_list(pid,account_head->account_list);
3272 GList* result = account_head->account_list;
3273 _ACCOUNT_FREE(account_head);
3279 int _account_delete(int pid, int account_id)
3281 int error_code = ACCOUNT_ERROR_NONE;
3282 account_stmt hstmt = NULL;
3283 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3285 int ret_transaction = 0;
3286 bool is_success = FALSE;
3288 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3291 /* Check requested ID to delete */
3292 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id=%d", ACCOUNT_TABLE, account_id);
3294 count = _account_get_record_count(query);
3296 if( _account_db_err_code() == SQLITE_PERM ){
3297 pthread_mutex_unlock(&account_mutex);
3298 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3299 return ACCOUNT_ERROR_PERMISSION_DENIED;
3303 ACCOUNT_ERROR("account id(%d) is not exist. count(%d)\n", account_id, count);
3304 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3307 /* Check permission of requested appid */
3308 char* current_appid = NULL;
3309 char *package_name = NULL;
3311 current_appid = _account_get_current_appid(pid);
3313 error_code = _account_get_package_name_from_account_id(account_id, &package_name);
3315 if(error_code != ACCOUNT_ERROR_NONE){
3316 ACCOUNT_ERROR("No package name with account_id\n");
3317 _ACCOUNT_FREE(current_appid);
3318 _ACCOUNT_FREE(package_name);
3319 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3321 ACCOUNT_DEBUG( "DELETE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name);
3323 error_code = _account_check_appid_group_with_package_name(current_appid, package_name);
3325 _ACCOUNT_FREE(current_appid);
3326 _ACCOUNT_FREE(package_name);
3328 if(error_code != ACCOUNT_ERROR_NONE){
3329 ACCOUNT_ERROR("No permission to delete\n");
3330 return ACCOUNT_ERROR_PERMISSION_DENIED;
3333 /* transaction control required*/
3334 ret_transaction = _account_begin_transaction();
3336 if( _account_db_err_code() == SQLITE_PERM ){
3337 pthread_mutex_unlock(&account_mutex);
3338 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3339 return ACCOUNT_ERROR_PERMISSION_DENIED;
3342 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
3343 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
3344 pthread_mutex_unlock(&account_mutex);
3345 return ACCOUNT_ERROR_DATABASE_BUSY;
3348 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3349 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3350 pthread_mutex_unlock(&account_mutex);
3351 return ret_transaction;
3354 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3355 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
3357 hstmt = _account_prepare_query(query);
3359 if( _account_db_err_code() == SQLITE_PERM ){
3360 pthread_mutex_unlock(&account_mutex);
3361 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3362 return ACCOUNT_ERROR_PERMISSION_DENIED;
3365 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3366 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3368 rc = _account_query_step(hstmt);
3369 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3371 rc = _account_query_finalize(hstmt);
3373 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3376 ACCOUNT_MEMSET(query, 0, sizeof(query));
3378 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
3380 hstmt = _account_prepare_query(query);
3381 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3382 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3384 rc = _account_query_step(hstmt);
3385 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
3387 rc = _account_query_finalize(hstmt);
3388 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3391 /* delete custom data */
3392 ACCOUNT_MEMSET(query, 0, sizeof(query));
3394 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
3396 hstmt = _account_prepare_query(query);
3398 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3399 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3401 rc = _account_query_step(hstmt);
3402 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
3404 rc = _account_query_finalize(hstmt);
3405 ACCOUNT_CATCH_ERROR(rc == ACCOUNT_ERROR_NONE, {}, rc, ("finalize error", account_id, rc));
3411 if (hstmt != NULL) {
3412 rc = _account_query_finalize(hstmt);
3413 if(rc != ACCOUNT_ERROR_NONE ){
3414 ACCOUNT_ERROR("rc (%d)", rc);
3421 ret_transaction = _account_end_transaction(is_success);
3423 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3424 ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3426 if (is_success == true) {
3428 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id);
3429 _account_insert_delete_update_notification_send(buf);
3433 pthread_mutex_unlock(&account_mutex);
3439 static int _account_query_account_by_username_and_package(const char* username, const char* package_name, account_h *account)
3443 int error_code = ACCOUNT_ERROR_NONE;
3444 account_stmt hstmt = NULL;
3445 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3447 int binding_count = 1;
3449 ACCOUNT_RETURN_VAL((username != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("username IS NULL"));
3450 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name IS NULL"));
3451 ACCOUNT_RETURN_VAL((*account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
3452 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3454 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3456 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
3457 hstmt = _account_prepare_query(query);
3459 if( _account_db_err_code() == SQLITE_PERM ){
3460 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3461 return ACCOUNT_ERROR_PERMISSION_DENIED;
3464 _account_query_bind_text(hstmt, binding_count++, username);
3465 _account_query_bind_text(hstmt, binding_count++, package_name);
3467 rc = _account_query_step(hstmt);
3468 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3470 account_s *account_record = (account_s *)(*account);
3472 while (rc == SQLITE_ROW) {
3473 _account_convert_column_to_account(hstmt, account_record);
3474 rc = _account_query_step(hstmt);
3477 rc = _account_query_finalize(hstmt);
3478 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3479 _account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
3480 _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record);
3483 error_code = ACCOUNT_ERROR_NONE;
3486 if (hstmt != NULL) {
3487 rc = _account_query_finalize(hstmt);
3488 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3492 pthread_mutex_unlock(&account_mutex);
3496 int _account_create(account_h *account)
3499 ACCOUNT_SLOGE("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
3500 return ACCOUNT_ERROR_INVALID_PARAMETER;
3503 account_s *data = (account_s*)malloc(sizeof(account_s));
3506 ACCOUNT_FATAL("Memory Allocation Failed");
3507 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3509 ACCOUNT_MEMSET(data, 0, sizeof(account_s));
3511 /*Setting account as visible by default*/
3512 data->secret = ACCOUNT_SECRECY_VISIBLE;
3514 /*Setting account as not supporting sync by default*/
3515 data->sync_support = ACCOUNT_SYNC_NOT_SUPPORT;
3517 *account = (account_h)data;
3519 return ACCOUNT_ERROR_NONE;
3522 int _account_destroy(account_h account)
3524 account_s *data = (account_s*)account;
3526 ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!"));
3528 _account_free_account_items(data);
3529 _ACCOUNT_FREE(data);
3531 return ACCOUNT_ERROR_NONE;
3534 int _account_get_account_id(account_s* account, int *account_id)
3537 return ACCOUNT_ERROR_INVALID_PARAMETER;
3540 return ACCOUNT_ERROR_INVALID_PARAMETER;
3543 *account_id = account->id;
3545 return ACCOUNT_ERROR_NONE;
3548 int _account_delete_from_db_by_user_name(int pid, const char *user_name, const char *package_name)
3550 _INFO("[%s][%s]", user_name, package_name);
3552 int error_code = ACCOUNT_ERROR_NONE;
3553 account_stmt hstmt = NULL;
3554 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3556 int ret_transaction = 0;
3557 bool is_success = FALSE;
3558 account_h account = NULL;
3559 int binding_count = 1;
3560 int account_id = -1;
3562 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is null!"));
3563 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
3564 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3566 /* Check permission of requested appid */
3567 char* current_appid = NULL;
3568 char* package_name_temp = NULL;
3570 current_appid = _account_get_current_appid(pid);
3572 package_name_temp = _account_get_text(package_name);
3574 ACCOUNT_DEBUG( "DELETE:user_name[%s],current_appid[%s], package_name[%s]", user_name, current_appid, package_name_temp);
3576 error_code = _account_check_appid_group_with_package_name(current_appid, package_name_temp);
3578 _ACCOUNT_FREE(current_appid);
3579 _ACCOUNT_FREE(package_name_temp);
3581 if(error_code != ACCOUNT_ERROR_NONE){
3582 ACCOUNT_ERROR("No permission to delete\n");
3583 return ACCOUNT_ERROR_PERMISSION_DENIED;
3586 rc = _account_create(&account);
3587 rc = _account_query_account_by_username_and_package(user_name, package_name, &account);
3591 if( _account_db_err_code() == SQLITE_PERM )
3593 _account_destroy(account);
3594 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3595 return ACCOUNT_ERROR_PERMISSION_DENIED;
3599 account_s* account_data = (account_s*)account;
3601 rc = _account_get_account_id(account_data, &account_id);
3603 rc = _account_destroy(account);
3605 /* transaction control required*/
3606 ret_transaction = _account_begin_transaction();
3608 if( _account_db_err_code() == SQLITE_PERM )
3610 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3611 pthread_mutex_unlock(&account_mutex);
3612 return ACCOUNT_ERROR_PERMISSION_DENIED;
3616 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY )
3618 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
3619 pthread_mutex_unlock(&account_mutex);
3620 return ACCOUNT_ERROR_DATABASE_BUSY;
3622 else if (ret_transaction != ACCOUNT_ERROR_NONE) {
3623 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3624 pthread_mutex_unlock(&account_mutex);
3625 return ret_transaction;
3628 /* delete custom data */
3629 ACCOUNT_MEMSET(query, 0, sizeof(query));
3630 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = ?", ACCOUNT_CUSTOM_TABLE);
3632 hstmt = _account_prepare_query(query);
3634 if( _account_db_err_code() == SQLITE_PERM ){
3635 _account_end_transaction(FALSE);
3636 pthread_mutex_unlock(&account_mutex);
3637 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3638 return ACCOUNT_ERROR_PERMISSION_DENIED;
3641 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3642 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3644 _account_query_bind_int(hstmt, binding_count++, account_id);
3646 rc = _account_query_step(hstmt);
3647 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3649 rc = _account_query_finalize(hstmt);
3650 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3653 /* delete capability */
3654 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", CAPABILITY_TABLE);
3656 hstmt = _account_prepare_query(query);
3658 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3659 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3662 _account_query_bind_text(hstmt, binding_count++, user_name);
3663 _account_query_bind_text(hstmt, binding_count++, package_name);
3665 rc = _account_query_step(hstmt);
3666 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3668 rc = _account_query_finalize(hstmt);
3669 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3672 ACCOUNT_MEMSET(query, 0, sizeof(query));
3675 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
3677 hstmt = _account_prepare_query(query);
3678 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3679 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3683 _account_query_bind_text(hstmt, binding_count++, user_name);
3684 _account_query_bind_text(hstmt, binding_count++, package_name);
3686 rc = _account_query_step(hstmt);
3687 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));
3689 rc = _account_query_finalize(hstmt);
3690 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3696 if (hstmt != NULL) {
3697 rc = _account_query_finalize(hstmt);
3698 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3702 ret_transaction = _account_end_transaction(is_success);
3704 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3705 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3707 if (is_success == true) {
3709 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id);
3710 _account_insert_delete_update_notification_send(buf);
3714 pthread_mutex_unlock(&account_mutex);
3719 int _account_delete_from_db_by_package_name(int pid, const char *package_name, gboolean permission)
3721 _INFO("_account_delete_from_db_by_package_name");
3722 int error_code = ACCOUNT_ERROR_NONE;
3723 account_stmt hstmt = NULL;
3724 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3726 int ret_transaction = 0;
3727 bool is_success = FALSE;
3728 int binding_count = 1;
3729 GSList *account_id_list = NULL;
3732 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
3733 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3735 /* Check permission of requested appid */
3737 char* current_appid = NULL;
3738 char* package_name_temp = NULL;
3740 current_appid = _account_get_current_appid(pid);
3742 package_name_temp = _account_get_text(package_name);
3744 ACCOUNT_DEBUG( "DELETE: current_appid[%s], package_name[%s]", current_appid, package_name_temp);
3746 error_code = _account_check_appid_group_with_package_name(current_appid, package_name_temp);
3748 _ACCOUNT_FREE(current_appid);
3749 _ACCOUNT_FREE(package_name_temp);
3751 if(error_code != ACCOUNT_ERROR_NONE){
3752 ACCOUNT_ERROR("No permission to delete\n");
3753 return ACCOUNT_ERROR_PERMISSION_DENIED;
3757 // It only needs list of ids, does not need to query sensitive info. So sending 0
3758 GList* account_list_temp = _account_query_account_by_package_name(getpid(), package_name, &ret);
3759 if (account_list_temp == NULL)
3761 _ERR("_account_query_account_by_package_name returned NULL");
3762 return ACCOUNT_ERROR_DB_FAILED;
3765 if( _account_db_err_code() == SQLITE_PERM ){
3766 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3767 return ACCOUNT_ERROR_PERMISSION_DENIED;
3770 if(ret != ACCOUNT_ERROR_NONE){
3774 account_list_temp = g_list_first(account_list_temp);
3775 _INFO("account_list_temp length=[%d]",g_list_length(account_list_temp));
3778 for (iter = account_list_temp; iter != NULL; iter = g_list_next(iter))
3780 _INFO("iterating account_list_temp");
3781 account_s *account = NULL;
3782 _INFO("Before iter->data");
3783 account = (account_s*)iter->data;
3784 _INFO("After iter->data");
3785 if (account != NULL)
3787 char id[256] = {0, };
3789 ACCOUNT_MEMSET(id, 0, 256);
3791 ACCOUNT_SNPRINTF(id, 256, "%d", account->id);
3793 _INFO("Adding account id [%s]", id);
3794 account_id_list = g_slist_append(account_id_list, g_strdup(id));
3798 /* transaction control required*/
3799 ret_transaction = _account_begin_transaction();
3801 if( _account_db_err_code() == SQLITE_PERM ){
3802 pthread_mutex_unlock(&account_mutex);
3803 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3804 return ACCOUNT_ERROR_PERMISSION_DENIED;
3807 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
3808 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
3809 pthread_mutex_unlock(&account_mutex);
3810 return ACCOUNT_ERROR_DATABASE_BUSY;
3811 }else if (ret_transaction != ACCOUNT_ERROR_NONE) {
3812 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3813 pthread_mutex_unlock(&account_mutex);
3814 return ret_transaction;
3817 /* delete custom table */
3818 ACCOUNT_MEMSET(query, 0, sizeof(query));
3819 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", ACCOUNT_CUSTOM_TABLE);
3821 hstmt = _account_prepare_query(query);
3823 if( _account_db_err_code() == SQLITE_PERM ){
3824 _account_end_transaction(FALSE);
3825 pthread_mutex_unlock(&account_mutex);
3826 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3827 return ACCOUNT_ERROR_PERMISSION_DENIED;
3830 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3831 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3834 _account_query_bind_text(hstmt, binding_count++, package_name);
3836 rc = _account_query_step(hstmt);
3837 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3839 rc = _account_query_finalize(hstmt);
3840 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3843 /* delete capability table */
3844 ACCOUNT_MEMSET(query, 0, sizeof(query));
3845 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", CAPABILITY_TABLE);
3847 hstmt = _account_prepare_query(query);
3849 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3850 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3853 _account_query_bind_text(hstmt, binding_count++, package_name);
3855 rc = _account_query_step(hstmt);
3856 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3858 rc = _account_query_finalize(hstmt);
3859 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3862 /* delete account table */
3863 ACCOUNT_MEMSET(query, 0, sizeof(query));
3865 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", ACCOUNT_TABLE);
3867 hstmt = _account_prepare_query(query);
3868 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3869 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3872 _account_query_bind_text(hstmt, binding_count++, package_name);
3874 rc = _account_query_step(hstmt);
3875 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));
3877 rc = _account_query_finalize(hstmt);
3878 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3884 if (hstmt != NULL) {
3885 rc = _account_query_finalize(hstmt);
3886 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3890 ret_transaction = _account_end_transaction(is_success);
3892 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3893 ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3895 if (is_success == true) {
3896 GSList* gs_iter = NULL;
3897 for (gs_iter = account_id_list; gs_iter != NULL; gs_iter = g_slist_next(gs_iter)) {
3898 char* p_tmpid = NULL;
3899 p_tmpid = (char*)gs_iter->data;
3901 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%s", ACCOUNT_NOTI_NAME_DELETE, p_tmpid);
3902 ACCOUNT_SLOGD("%s", buf);
3903 _account_insert_delete_update_notification_send(buf);
3904 _ACCOUNT_FREE(p_tmpid);
3906 g_slist_free(account_id_list);
3910 pthread_mutex_unlock(&account_mutex);
3912 _INFO("_account_delete_from_db_by_package_name end");
3916 int _account_get_total_count_from_db(gboolean include_hidden, int *count)
3919 ACCOUNT_SLOGE("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__);
3920 return ACCOUNT_ERROR_INVALID_PARAMETER;
3924 ACCOUNT_ERROR("DB is not opened\n");
3925 return ACCOUNT_ERROR_DB_NOT_OPENED;
3928 char query[1024] = {0, };
3929 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3933 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s", ACCOUNT_TABLE);
3937 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where secret = %d", ACCOUNT_TABLE, ACCOUNT_SECRECY_VISIBLE);
3940 *count = _account_get_record_count(query);
3942 if( _account_db_err_code() == SQLITE_PERM ){
3943 pthread_mutex_unlock(&account_mutex);
3944 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3945 return ACCOUNT_ERROR_PERMISSION_DENIED;
3950 account_stmt pStmt = NULL;
3952 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
3953 if (SQLITE_OK != rc) {
3954 ACCOUNT_SLOGE("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg());
3955 sqlite3_finalize(pStmt);
3956 return ACCOUNT_ERROR_DB_FAILED;
3959 rc = sqlite3_step(pStmt);
3960 if (SQLITE_ROW != rc) {
3961 ACCOUNT_ERROR("[ERROR] sqlite3_step() failed\n");
3962 sqlite3_finalize(pStmt);
3963 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3966 ncount = sqlite3_column_int(pStmt, 0);
3970 sqlite3_finalize(pStmt);
3973 ACCOUNT_ERROR("[ERROR] Number of account : %d, End", ncount);
3974 return ACCOUNT_ERROR_DB_FAILED;
3977 return ACCOUNT_ERROR_NONE;
3980 int account_type_create(account_type_h *account_type)
3982 if (!account_type) {
3983 ACCOUNT_SLOGE("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
3984 return ACCOUNT_ERROR_INVALID_PARAMETER;
3987 account_type_s *data = (account_type_s*)malloc(sizeof(account_type_s));
3990 ACCOUNT_ERROR("Memory Allocation Failed");
3991 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3994 ACCOUNT_MEMSET(data, 0, sizeof(account_type_s));
3996 *account_type = (account_type_h)data;
3998 return ACCOUNT_ERROR_NONE;
4001 int account_type_destroy(account_type_h account_type)
4003 account_type_s *data = (account_type_s*)account_type;
4005 ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account type handle is null!"));
4007 _account_type_free_account_type_items(data);
4008 _ACCOUNT_FREE(data);
4010 return ACCOUNT_ERROR_NONE;
4013 //app_id mandatory field
4014 int account_type_set_app_id(account_type_h account_type, const char *app_id)
4016 if (!account_type) {
4017 ACCOUNT_SLOGE("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
4018 return ACCOUNT_ERROR_INVALID_PARAMETER;
4022 ACCOUNT_SLOGE("(%s)-(%d) app_id is NULL.\n", __FUNCTION__, __LINE__);
4023 return ACCOUNT_ERROR_INVALID_PARAMETER;
4026 account_type_s *data = (account_type_s*)account_type;
4028 _ACCOUNT_FREE(data->app_id);
4029 data->app_id = _account_get_text(app_id);
4031 return ACCOUNT_ERROR_NONE;
4034 //service_provider_id mandatory field
4035 int account_type_set_service_provider_id(account_type_h account_type, const char *service_provider_id)
4037 if (!account_type) {
4038 return ACCOUNT_ERROR_INVALID_PARAMETER;
4041 if (!service_provider_id) {
4042 return ACCOUNT_ERROR_INVALID_PARAMETER;
4045 account_type_s *data = (account_type_s*)account_type;
4047 _ACCOUNT_FREE(data->service_provider_id);
4048 data->service_provider_id = _account_get_text(service_provider_id);
4050 return ACCOUNT_ERROR_NONE;
4053 int account_type_set_icon_path(account_type_h account_type, const char *icon_path)
4055 if (!account_type) {
4056 return ACCOUNT_ERROR_INVALID_PARAMETER;
4060 return ACCOUNT_ERROR_INVALID_PARAMETER;
4063 account_type_s *data = (account_type_s*)account_type;
4065 _ACCOUNT_FREE(data->icon_path);
4066 data->icon_path = _account_get_text(icon_path);
4068 return ACCOUNT_ERROR_NONE;
4071 int account_type_set_small_icon_path(account_type_h account_type, const char *small_icon_path)
4073 if (!account_type) {
4074 return ACCOUNT_ERROR_INVALID_PARAMETER;
4077 if (!small_icon_path) {
4078 return ACCOUNT_ERROR_INVALID_PARAMETER;
4081 account_type_s *data = (account_type_s*)account_type;
4083 _ACCOUNT_FREE(data->small_icon_path);
4084 data->small_icon_path = _account_get_text(small_icon_path);
4086 return ACCOUNT_ERROR_NONE;
4089 int account_type_set_multiple_account_support(account_type_h account_type, const bool multiple_account_support)
4091 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
4093 account_type_s *data = (account_type_s*)account_type;
4095 data->multiple_account_support = multiple_account_support;
4097 return ACCOUNT_ERROR_NONE;
4101 int account_type_set_label(account_type_h account_type, const char* label, const char* locale)
4103 if (!account_type) {
4104 ACCOUNT_SLOGE("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
4105 return ACCOUNT_ERROR_INVALID_PARAMETER;
4108 if(!label || !locale) {
4109 return ACCOUNT_ERROR_INVALID_PARAMETER;
4112 account_type_s *data = (account_type_s*)account_type;
4113 label_s *label_data = (label_s*)malloc(sizeof(label_s));
4115 if (label_data == NULL) {
4116 return ACCOUNT_ERROR_OUT_OF_MEMORY;
4118 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
4120 label_data->label = _account_get_text(label);
4121 label_data->locale = _account_get_text(locale);
4123 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
4125 return ACCOUNT_ERROR_NONE;
4128 int account_type_get_app_id(account_type_h account_type, char **app_id)
4130 if (!account_type) {
4131 return ACCOUNT_ERROR_INVALID_PARAMETER;
4135 return ACCOUNT_ERROR_INVALID_PARAMETER;
4138 account_type_s *data = (account_type_s*)account_type;
4141 *app_id = _account_get_text(data->app_id);
4143 return ACCOUNT_ERROR_NONE;
4146 int account_type_get_service_provider_id(account_type_h account_type, char **service_provider_id)
4148 if (!account_type) {
4149 return ACCOUNT_ERROR_INVALID_PARAMETER;
4152 if (!service_provider_id) {
4153 return ACCOUNT_ERROR_INVALID_PARAMETER;
4156 account_type_s *data = (account_type_s*)account_type;
4158 (*service_provider_id) = NULL;
4159 *service_provider_id = _account_get_text(data->service_provider_id);
4161 return ACCOUNT_ERROR_NONE;
4164 int account_type_get_icon_path(account_type_h account_type, char **icon_path)
4166 if (!account_type) {
4167 return ACCOUNT_ERROR_INVALID_PARAMETER;
4171 return ACCOUNT_ERROR_INVALID_PARAMETER;
4174 account_type_s *data = (account_type_s*)account_type;
4176 (*icon_path) = NULL;
4177 *icon_path = _account_get_text(data->icon_path);
4179 return ACCOUNT_ERROR_NONE;
4182 int account_type_get_small_icon_path(account_type_h account_type, char **small_icon_path)
4184 if (!account_type) {
4185 return ACCOUNT_ERROR_INVALID_PARAMETER;
4188 if (!small_icon_path) {
4189 return ACCOUNT_ERROR_INVALID_PARAMETER;
4192 account_type_s *data = (account_type_s*)account_type;
4194 (*small_icon_path) = NULL;
4195 *small_icon_path = _account_get_text(data->small_icon_path);
4197 return ACCOUNT_ERROR_NONE;
4200 int account_type_get_multiple_account_support(account_type_h account_type, int *multiple_account_support)
4202 if (!account_type) {
4203 return ACCOUNT_ERROR_INVALID_PARAMETER;
4205 if (!multiple_account_support) {
4206 return ACCOUNT_ERROR_INVALID_PARAMETER;
4209 account_type_s *data = (account_type_s*)account_type;
4211 *multiple_account_support = data->multiple_account_support;
4213 return ACCOUNT_ERROR_NONE;
4216 int account_type_get_label_by_locale(account_type_h account_type, const char* locale, char** label)
4218 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4219 ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID PARAMETER"));
4222 account_type_s *data = (account_type_s*)account_type;
4224 for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) {
4225 label_s *label_data = NULL;
4227 label_data = (label_s*)iter->data;
4231 if(!strcmp(locale, label_data->locale)) {
4232 *label = _account_get_text(label_data->label);
4233 return ACCOUNT_ERROR_NONE;
4237 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4240 int account_type_get_label(account_type_h account_type, account_label_cb callback, void *user_data)
4242 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4243 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4246 account_type_s *data = (account_type_s*)account_type;
4248 for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) {
4249 label_s *label_data = NULL;
4251 label_data = (label_s*)iter->data;
4253 if(callback(label_data->app_id, label_data->label, label_data->locale, user_data)!=TRUE) {
4254 ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n");
4255 return ACCOUNT_ERROR_NONE;
4259 return ACCOUNT_ERROR_NONE;
4262 static gboolean _account_type_check_duplicated(account_type_s *data)
4264 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4267 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4269 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId='%s'"
4270 , ACCOUNT_TYPE_TABLE, data->app_id);
4272 count = _account_get_record_count(query);
4280 static int _account_type_convert_account_to_sql(account_type_s *account_type, account_stmt hstmt, char *sql_value)
4286 /*Caution : Keep insert query orders.*/
4289 _account_query_bind_text(hstmt, count++, (char*)account_type->app_id);
4291 /* 2. service provider id*/
4292 _account_query_bind_text(hstmt, count++, (char*)account_type->service_provider_id);
4295 _account_query_bind_text(hstmt, count++, (char*)account_type->icon_path);
4297 /* 4. small icon path*/
4298 _account_query_bind_text(hstmt, count++, (char*)account_type->small_icon_path);
4300 /* 5. multiple accont support*/
4301 _account_query_bind_int(hstmt, count++, account_type->multiple_account_support);
4309 static int _account_type_execute_insert_query(account_type_s *account_type)
4314 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4315 int error_code = ACCOUNT_ERROR_NONE;
4316 account_stmt hstmt = NULL;
4318 /* check mandatory field */
4319 // app id & service provider id
4320 if (!account_type->app_id) {
4321 return ACCOUNT_ERROR_INVALID_PARAMETER;
4324 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4325 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s( AppId, ServiceProviderId , IconPath , SmallIconPath , MultipleAccountSupport ) values "
4326 "(?, ?, ?, ?, ?)", ACCOUNT_TYPE_TABLE);
4329 hstmt = _account_prepare_query(query);
4332 if( _account_db_err_code() == SQLITE_PERM ){
4333 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4334 return ACCOUNT_ERROR_PERMISSION_DENIED;
4335 } else if( _account_db_err_code() == SQLITE_BUSY ){
4336 ACCOUNT_ERROR( "Database Busy(%s)", _account_db_err_msg());
4337 return ACCOUNT_ERROR_DATABASE_BUSY;
4340 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4343 _account_type_convert_account_to_sql(account_type, hstmt, query);
4346 rc = _account_query_step(hstmt);
4347 if (rc == SQLITE_BUSY) {
4348 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4349 error_code = ACCOUNT_ERROR_DATABASE_BUSY;
4350 } else if (rc != SQLITE_DONE) {
4351 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4352 error_code = ACCOUNT_ERROR_DB_FAILED;
4356 rc = _account_query_finalize(hstmt);
4357 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4364 static int _account_type_insert_label(account_type_s *account_type)
4367 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4368 account_stmt hstmt = NULL;
4370 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4372 if (g_slist_length( account_type->label_list)==0) {
4373 ACCOUNT_ERROR( "_account_type_insert_label, no label\n");
4374 return ACCOUNT_ERROR_NONE;
4377 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId = '%s'", ACCOUNT_TYPE_TABLE, account_type->app_id);
4379 rc = _account_get_record_count(query);
4381 if( _account_db_err_code() == SQLITE_PERM ){
4382 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4383 return ACCOUNT_ERROR_PERMISSION_DENIED;
4387 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4393 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
4396 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4397 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
4398 "(?, ?, ?) ", LABEL_TABLE);
4400 hstmt = _account_prepare_query(query);
4402 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4404 label_s* label_data = NULL;
4405 label_data = (label_s*)iter->data;
4407 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4408 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4409 ret = _account_query_bind_text(hstmt, count++, label_data->label);
4410 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4411 ret = _account_query_bind_text(hstmt, count++, (char*)label_data->locale);
4412 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4414 rc = _account_query_step(hstmt);
4416 if (rc != SQLITE_DONE) {
4417 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4421 rc = _account_query_finalize(hstmt);
4422 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4427 return ACCOUNT_ERROR_NONE;
4430 static void _account_type_convert_column_to_provider_feature(account_stmt hstmt, provider_feature_s *feature_record)
4432 const char *textbuf = NULL;
4434 textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_APP_ID);
4435 _account_db_data_to_text(textbuf, &(feature_record->app_id));
4437 textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_KEY);
4438 _account_db_data_to_text(textbuf, &(feature_record->key));
4442 GSList* _account_type_query_provider_feature_by_app_id(const char* app_id, int *error_code)
4444 _INFO("_account_type_query_provider_feature_by_app_id");
4445 *error_code = ACCOUNT_ERROR_NONE;
4446 account_stmt hstmt = NULL;
4447 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4448 int rc = 0, binding_count = 1;
4449 GSList* feature_list = NULL;
4451 ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
4452 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
4454 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4456 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
4457 _INFO("account query=[%s]", query);
4459 hstmt = _account_prepare_query(query);
4461 if( _account_db_err_code() == SQLITE_PERM ){
4462 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4463 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
4467 _account_query_bind_text(hstmt, binding_count++, app_id);
4469 rc = _account_query_step(hstmt);
4470 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {*error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;}, NULL, ("The record isn't found.\n"));
4472 provider_feature_s* feature_record = NULL;
4474 while (rc == SQLITE_ROW) {
4476 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
4478 if (feature_record == NULL) {
4479 ACCOUNT_FATAL("malloc Failed");
4483 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
4485 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
4487 _INFO("Adding account feature_list");
4488 feature_list = g_slist_append(feature_list, feature_record);
4490 rc = _account_query_step(hstmt);
4493 rc = _account_query_finalize(hstmt);
4494 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("account finalize error"));
4497 *error_code = ACCOUNT_ERROR_NONE;
4500 if (hstmt != NULL) {
4501 rc = _account_query_finalize(hstmt);
4502 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("account finalize error"));
4506 pthread_mutex_unlock(&account_mutex);
4508 _INFO("Returning account feature_list");
4509 return feature_list;
4512 int account_type_query_provider_feature_by_app_id(provider_feature_cb callback, const char* app_id, void *user_data )
4514 int error_code = ACCOUNT_ERROR_NONE;
4515 account_stmt hstmt = NULL;
4516 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4517 int rc = 0, binding_count = 1;
4519 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4520 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4521 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4523 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4525 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
4526 hstmt = _account_prepare_query(query);
4528 if( _account_db_err_code() == SQLITE_PERM ){
4529 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4530 return ACCOUNT_ERROR_PERMISSION_DENIED;
4533 _account_query_bind_text(hstmt, binding_count++, app_id);
4535 rc = _account_query_step(hstmt);
4536 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4538 provider_feature_s* feature_record = NULL;
4540 while (rc == SQLITE_ROW) {
4541 bool cb_ret = FALSE;
4542 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
4544 if (feature_record == NULL) {
4545 ACCOUNT_FATAL("malloc Failed");
4549 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
4551 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
4553 cb_ret = callback(feature_record->app_id, feature_record->key, user_data);
4555 _account_type_free_feature_items(feature_record);
4556 _ACCOUNT_FREE(feature_record);
4558 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
4560 rc = _account_query_step(hstmt);
4563 rc = _account_query_finalize(hstmt);
4564 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4567 error_code = ACCOUNT_ERROR_NONE;
4570 if (hstmt != NULL) {
4571 rc = _account_query_finalize(hstmt);
4572 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4576 pthread_mutex_unlock(&account_mutex);
4580 bool _account_type_query_supported_feature(const char* app_id, const char* capability, int *error_code)
4582 _INFO("_account_type_query_supported_feature start");
4584 *error_code = ACCOUNT_ERROR_NONE;
4586 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4587 int record_count = 0;
4589 if (app_id == NULL || capability == NULL)
4591 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
4595 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
4597 record_count = _account_get_record_count(query);
4599 if( _account_db_err_code() == SQLITE_PERM ){
4600 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4601 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
4605 if (record_count <= 0)
4607 *error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
4611 _INFO("_account_type_query_supported_feature end");
4617 int account_type_get_provider_feature_all(account_type_h account_type, provider_feature_cb callback, void* user_data)
4619 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4620 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4623 account_type_s *data = (account_type_s*)account_type;
4625 for (iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4626 provider_feature_s *feature_data = NULL;
4628 feature_data = (provider_feature_s*)iter->data;
4630 if(callback(feature_data->app_id, feature_data->key, user_data)!=TRUE) {
4631 ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n");
4632 return ACCOUNT_ERROR_NONE;
4636 return ACCOUNT_ERROR_NONE;
4639 int account_type_set_provider_feature(account_type_h account_type, const char* provider_feature)
4641 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account type handle is null"));
4642 ACCOUNT_RETURN_VAL((provider_feature != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("provider_feature is null"));
4644 account_type_s *data = (account_type_s*)account_type;
4646 GSList *iter = NULL;
4647 bool b_is_new = TRUE;
4649 for(iter = data->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4650 provider_feature_s *feature_data = NULL;
4651 feature_data = (provider_feature_s*)iter->data;
4653 if(!strcmp(feature_data->key, provider_feature)) {
4660 provider_feature_s* feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
4662 if (feature_data == NULL)
4663 return ACCOUNT_ERROR_OUT_OF_MEMORY;
4664 ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
4666 feature_data->key = _account_get_text(provider_feature);
4667 data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
4670 return ACCOUNT_ERROR_NONE;
4673 static int _account_type_insert_provider_feature(account_type_s *account_type, const char* app_id)
4676 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4677 account_stmt hstmt = NULL;
4679 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4680 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4682 if (g_slist_length( account_type->provider_feature_list)==0) {
4683 ACCOUNT_ERROR( "no capability\n");
4684 return ACCOUNT_ERROR_NONE;
4687 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId='%s'", ACCOUNT_TYPE_TABLE, app_id);
4689 rc = _account_get_record_count(query);
4691 if( _account_db_err_code() == SQLITE_PERM ){
4692 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4693 return ACCOUNT_ERROR_PERMISSION_DENIED;
4697 ACCOUNT_SLOGI( "related account type item is not existed rc=%d , %s", rc, _account_db_err_msg());
4698 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4705 for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4708 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4709 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
4710 "(?, ?) ", PROVIDER_FEATURE_TABLE);
4712 hstmt = _account_prepare_query(query);
4714 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4716 provider_feature_s* feature_data = NULL;
4717 feature_data = (provider_feature_s*)iter->data;
4719 ret = _account_query_bind_text(hstmt, count++, app_id);
4720 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4721 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
4722 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
4724 rc = _account_query_step(hstmt);
4726 if (rc != SQLITE_DONE) {
4727 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4731 rc = _account_query_finalize(hstmt);
4732 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4737 return ACCOUNT_ERROR_NONE;
4740 int _account_type_insert_to_db(account_type_s* account_type, int* account_type_id)
4744 int error_code = ACCOUNT_ERROR_NONE, ret_transaction = 0;
4746 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4747 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL"));
4748 ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID POINTER IS NULL"));
4750 account_type_s *data = (account_type_s*)account_type;
4752 pthread_mutex_lock(&account_mutex);
4755 /* transaction control required*/
4756 ret_transaction = _account_begin_transaction();
4760 if( _account_db_err_code() == SQLITE_PERM ){
4761 pthread_mutex_unlock(&account_mutex);
4762 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4763 return ACCOUNT_ERROR_PERMISSION_DENIED;
4767 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
4768 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
4769 pthread_mutex_unlock(&account_mutex);
4770 return ACCOUNT_ERROR_DATABASE_BUSY;
4771 } else if (ret_transaction != ACCOUNT_ERROR_NONE) {
4772 ACCOUNT_ERROR("_account_begin_transaction fail %d\n", ret_transaction);
4773 pthread_mutex_unlock(&account_mutex);
4774 return ret_transaction;
4778 if (_account_type_check_duplicated(data)) {
4780 ret_transaction = _account_end_transaction(FALSE);
4781 ACCOUNT_ERROR("Duplicated, rollback insert query(%x)!!!!\n", ret_transaction);
4782 *account_type_id = -1;
4783 pthread_mutex_unlock(&account_mutex);
4784 return ACCOUNT_ERROR_DUPLICATED;
4787 *account_type_id = _account_get_next_sequence(ACCOUNT_TYPE_TABLE);
4789 error_code = _account_type_execute_insert_query(data);
4791 if (error_code != ACCOUNT_ERROR_NONE){
4792 error_code = ACCOUNT_ERROR_DUPLICATED;
4793 ret_transaction = _account_end_transaction(FALSE);
4794 ACCOUNT_ERROR("Insert fail, rollback insert query(%x)!!!!\n", ret_transaction);
4795 *account_type_id = -1;
4796 pthread_mutex_unlock(&account_mutex);
4802 error_code = _account_type_insert_provider_feature(data, data->app_id);
4803 if(error_code != ACCOUNT_ERROR_NONE) {
4805 ret_transaction = _account_end_transaction(FALSE);
4806 ACCOUNT_ERROR("Insert provider feature fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
4807 pthread_mutex_unlock(&account_mutex);
4811 error_code = _account_type_insert_label(data);
4812 if(error_code != ACCOUNT_ERROR_NONE) {
4814 ret_transaction = _account_end_transaction(FALSE);
4815 ACCOUNT_ERROR("Insert label fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
4816 pthread_mutex_unlock(&account_mutex);
4820 ret_transaction = _account_end_transaction(TRUE);
4822 pthread_mutex_unlock(&account_mutex);
4825 return ACCOUNT_ERROR_NONE;
4828 static int _account_type_update_provider_feature(account_type_s *account_type, const char* app_id)
4831 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4832 account_stmt hstmt = NULL;
4834 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4836 if (g_slist_length( account_type->provider_feature_list)==0) {
4837 ACCOUNT_ERROR( "no feature\n");
4838 return ACCOUNT_ERROR_NONE;
4841 ACCOUNT_DEBUG( "app id", app_id);
4843 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4845 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id=? ", PROVIDER_FEATURE_TABLE);
4846 hstmt = _account_prepare_query(query);
4848 if( _account_db_err_code() == SQLITE_PERM ){
4849 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4850 return ACCOUNT_ERROR_PERMISSION_DENIED;
4854 _account_query_bind_text(hstmt, count++, app_id);
4855 rc = _account_query_step(hstmt);
4857 if (rc != SQLITE_DONE) {
4858 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4859 return ACCOUNT_ERROR_DB_FAILED;
4861 rc = _account_query_finalize(hstmt);
4862 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4867 for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
4870 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4871 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
4872 "(?, ?) ", PROVIDER_FEATURE_TABLE);
4874 hstmt = _account_prepare_query(query);
4876 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4878 provider_feature_s* feature_data = NULL;
4879 feature_data = (provider_feature_s*)iter->data;
4881 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4882 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4883 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
4884 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4886 rc = _account_query_step(hstmt);
4888 if (rc != SQLITE_DONE) {
4889 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4892 rc = _account_query_finalize(hstmt);
4893 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4897 return ACCOUNT_ERROR_NONE;
4900 static int _account_type_update_label(account_type_s *account_type, const char* app_id)
4903 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4904 account_stmt hstmt = NULL;
4906 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4908 if (g_slist_length( account_type->label_list)==0) {
4909 return ACCOUNT_ERROR_NONE;
4912 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4914 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId=? ", LABEL_TABLE);
4915 hstmt = _account_prepare_query(query);
4917 if( _account_db_err_code() == SQLITE_PERM ){
4918 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4919 return ACCOUNT_ERROR_PERMISSION_DENIED;
4923 _account_query_bind_text(hstmt, count++, app_id);
4924 rc = _account_query_step(hstmt);
4926 if (rc != SQLITE_DONE) {
4927 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4928 return ACCOUNT_ERROR_DB_FAILED;
4930 rc = _account_query_finalize(hstmt);
4931 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4936 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
4939 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4940 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
4941 "(?, ?, ?) ", LABEL_TABLE);
4943 hstmt = _account_prepare_query(query);
4945 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4947 label_s* label_data = NULL;
4948 label_data = (label_s*)iter->data;
4950 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4951 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4952 ret = _account_query_bind_text(hstmt, count++, label_data->label);
4953 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4954 ret = _account_query_bind_text(hstmt, count++, label_data->locale);
4955 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4957 rc = _account_query_step(hstmt);
4959 if (rc != SQLITE_DONE) {
4960 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4963 rc = _account_query_finalize(hstmt);
4964 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4968 return ACCOUNT_ERROR_NONE;
4972 static int _account_type_update_account(account_type_s *account_type, const char* app_id)
4974 int rc = 0, binding_count =1;
4975 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4976 int error_code = ACCOUNT_ERROR_NONE;
4977 account_stmt hstmt = NULL;
4979 if (!account_type->app_id) {
4980 ACCOUNT_ERROR("app id is mandetory field, it can not be NULL!!!!\n");
4981 return ACCOUNT_ERROR_INVALID_PARAMETER;
4984 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4985 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET AppId=?, ServiceProviderId=?, IconPath=?, "
4986 "SmallIconPath=?, MultipleAccountSupport=? WHERE AppId=? ", ACCOUNT_TYPE_TABLE);
4988 hstmt = _account_prepare_query(query);
4990 if( _account_db_err_code() == SQLITE_PERM ){
4991 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4992 return ACCOUNT_ERROR_PERMISSION_DENIED;
4993 } else if (_account_db_err_code() == SQLITE_BUSY){
4994 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
4995 return ACCOUNT_ERROR_DATABASE_BUSY;
4998 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
5000 binding_count = _account_type_convert_account_to_sql(account_type, hstmt, query);
5001 _account_query_bind_text(hstmt, binding_count++, app_id);
5003 rc = _account_query_step(hstmt);
5004 if (rc != SQLITE_DONE) {
5005 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5008 rc = _account_query_finalize(hstmt);
5009 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5013 error_code = _account_type_update_label(account_type, app_id);
5014 /* update provider feature */
5015 error_code = _account_type_update_provider_feature(account_type, app_id);
5020 int _account_type_update_to_db_by_app_id(account_type_s* account_type, const char* app_id)
5022 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
5023 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5024 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5026 int error_code = ACCOUNT_ERROR_NONE;
5027 account_type_s* data = account_type;
5029 pthread_mutex_lock(&account_mutex);
5031 error_code = _account_type_update_account(data, app_id);
5033 pthread_mutex_unlock(&account_mutex);
5038 int _account_type_delete_by_app_id(const char* app_id)
5040 int error_code = ACCOUNT_ERROR_NONE;
5041 account_stmt hstmt = NULL;
5042 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5043 int rc = 0, count = -1;
5044 int ret_transaction = 0;
5045 int binding_count = 1;
5046 bool is_success = FALSE;
5048 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5049 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("The database isn't connected."));
5051 /* Check requested ID to delete */
5052 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
5054 count = _account_get_record_count(query);
5056 if( _account_db_err_code() == SQLITE_PERM ){
5057 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5058 return ACCOUNT_ERROR_PERMISSION_DENIED;
5062 ACCOUNT_SLOGE("app id(%s) is not exist. count(%d)\n", app_id, count);
5063 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5066 /* transaction control required*/
5067 ret_transaction = _account_begin_transaction();
5069 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
5070 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
5071 pthread_mutex_unlock(&account_mutex);
5072 return ACCOUNT_ERROR_DATABASE_BUSY;
5073 }else if (ret_transaction != ACCOUNT_ERROR_NONE) {
5074 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
5075 pthread_mutex_unlock(&account_mutex);
5076 return ret_transaction;
5079 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", LABEL_TABLE);
5081 hstmt = _account_prepare_query(query);
5083 if( _account_db_err_code() == SQLITE_PERM ){
5084 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5085 pthread_mutex_unlock(&account_mutex);
5086 return ACCOUNT_ERROR_PERMISSION_DENIED;
5089 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
5090 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
5092 _account_query_bind_text(hstmt, binding_count++, app_id);
5094 rc = _account_query_step(hstmt);
5095 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5097 rc = _account_query_finalize(hstmt);
5098 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5102 ACCOUNT_MEMSET(query, 0, sizeof(query));
5104 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id = ? ", PROVIDER_FEATURE_TABLE);
5106 hstmt = _account_prepare_query(query);
5107 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
5108 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
5110 _account_query_bind_text(hstmt, binding_count++, app_id);
5112 rc = _account_query_step(hstmt);
5113 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
5115 rc = _account_query_finalize(hstmt);
5116 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5122 ACCOUNT_MEMSET(query, 0, sizeof(query));
5124 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ? ", ACCOUNT_TYPE_TABLE);
5126 hstmt = _account_prepare_query(query);
5127 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
5128 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
5130 _account_query_bind_text(hstmt, binding_count++, app_id);
5132 rc = _account_query_step(hstmt);
5133 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
5135 rc = _account_query_finalize(hstmt);
5136 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5142 if (hstmt != NULL) {
5143 rc = _account_query_finalize(hstmt);
5144 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5148 ret_transaction = _account_end_transaction(is_success);
5150 if (ret_transaction != ACCOUNT_ERROR_NONE) {
5151 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
5154 pthread_mutex_unlock(&account_mutex);
5159 static void _account_type_convert_column_to_account_type(account_stmt hstmt, account_type_s *account_type_record)
5161 const char *textbuf = NULL;
5163 account_type_record->id = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_ID);
5165 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_APP_ID);
5166 _account_db_data_to_text(textbuf, &(account_type_record->app_id));
5168 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SERVICE_PROVIDER_ID);
5169 _account_db_data_to_text(textbuf, &(account_type_record->service_provider_id));
5171 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_ICON_PATH);
5172 _account_db_data_to_text(textbuf, &(account_type_record->icon_path));
5174 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SMALL_ICON_PATH);
5175 _account_db_data_to_text(textbuf, &(account_type_record->small_icon_path));
5177 account_type_record->multiple_account_support = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_MULTIPLE_ACCOUNT_SUPPORT);
5181 static void _account_type_convert_column_to_label(account_stmt hstmt, label_s *label_record)
5183 const char *textbuf = NULL;
5185 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_APP_ID);
5186 _account_db_data_to_text(textbuf, &(label_record->app_id));
5188 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LABEL);
5189 _account_db_data_to_text(textbuf, &(label_record->label));
5191 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LOCALE);
5192 _account_db_data_to_text(textbuf, &(label_record->locale));
5196 GSList* _account_type_get_label_list_by_app_id(const char* app_id, int *error_code )
5198 *error_code = ACCOUNT_ERROR_NONE;
5199 account_stmt hstmt = NULL;
5200 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5201 int rc = 0, binding_count = 1;
5202 GSList* label_list = NULL;
5204 ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
5205 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
5207 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5209 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
5210 hstmt = _account_prepare_query(query);
5212 if( _account_db_err_code() == SQLITE_PERM ){
5213 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5214 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
5218 _account_query_bind_text(hstmt, binding_count++, app_id);
5220 rc = _account_query_step(hstmt);
5221 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5223 label_s* label_record = NULL;
5225 while (rc == SQLITE_ROW) {
5226 label_record = (label_s*) malloc(sizeof(label_s));
5228 if (label_record == NULL) {
5229 ACCOUNT_FATAL("malloc Failed");
5233 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
5235 _account_type_convert_column_to_label(hstmt, label_record);
5237 _INFO("Adding account label_list");
5238 label_list = g_slist_append (label_list, label_record);
5240 rc = _account_query_step(hstmt);
5243 rc = _account_query_finalize(hstmt);
5244 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
5247 *error_code = ACCOUNT_ERROR_NONE;
5250 if (hstmt != NULL) {
5251 rc = _account_query_finalize(hstmt);
5252 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
5256 pthread_mutex_unlock(&account_mutex);
5257 _INFO("Returning account label_list");
5261 int account_type_query_label_by_app_id(account_label_cb callback, const char* app_id, void *user_data )
5263 int error_code = ACCOUNT_ERROR_NONE;
5264 account_stmt hstmt = NULL;
5265 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5266 int rc = 0, binding_count = 1;
5268 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5269 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
5270 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5272 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5274 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
5275 hstmt = _account_prepare_query(query);
5277 if( _account_db_err_code() == SQLITE_PERM ){
5278 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5279 return ACCOUNT_ERROR_PERMISSION_DENIED;
5282 _account_query_bind_text(hstmt, binding_count++, app_id);
5284 rc = _account_query_step(hstmt);
5285 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5287 label_s* label_record = NULL;
5289 while (rc == SQLITE_ROW) {
5290 bool cb_ret = FALSE;
5291 label_record = (label_s*) malloc(sizeof(label_s));
5293 if (label_record == NULL) {
5294 ACCOUNT_FATAL("malloc Failed");
5298 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
5300 _account_type_convert_column_to_label(hstmt, label_record);
5302 cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data);
5304 _account_type_free_label_items(label_record);
5305 _ACCOUNT_FREE(label_record);
5307 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
5309 rc = _account_query_step(hstmt);
5312 rc = _account_query_finalize(hstmt);
5313 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5316 error_code = ACCOUNT_ERROR_NONE;
5319 if (hstmt != NULL) {
5320 rc = _account_query_finalize(hstmt);
5321 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5325 pthread_mutex_unlock(&account_mutex);
5329 int _account_type_label_get_app_id(label_h label, char **app_id)
5332 return ACCOUNT_ERROR_INVALID_PARAMETER;
5336 return ACCOUNT_ERROR_INVALID_PARAMETER;
5339 label_s *data = (label_s*)label;
5343 *app_id = _account_get_text(data->app_id);
5345 return ACCOUNT_ERROR_NONE;
5348 bool _account_get_label_text_cb(char* app_id, char* label, char* locale, void *user_data)
5350 account_type_s *data = (account_type_s*)user_data;
5352 label_s *label_data = (label_s*)malloc(sizeof(label_s));
5354 if (label_data == NULL) {
5355 ACCOUNT_FATAL("_account_get_label_text_cb : MALLOC FAIL\n");
5358 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
5360 label_data->app_id = _account_get_text(app_id);
5361 label_data->label = _account_get_text(label);
5362 label_data->locale = _account_get_text(locale);
5364 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
5369 bool _account_get_provider_feature_cb(char* app_id, char* key, void* user_data)
5371 account_type_s *data = (account_type_s*)user_data;
5373 provider_feature_s *feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
5375 if (feature_data == NULL) {
5376 ACCOUNT_FATAL("_account_get_provider_feature_cb : MALLOC FAIL\n");
5379 ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
5381 feature_data->app_id = _account_get_text(app_id);
5382 feature_data->key = _account_get_text(key);
5384 data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
5389 int _account_type_query_by_app_id(const char* app_id, account_type_s** account_type_record)
5391 _INFO("_account_type_query_by_app_id start");
5393 int error_code = ACCOUNT_ERROR_NONE;
5394 account_stmt hstmt = NULL;
5395 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5396 int rc = 0, binding_count = 1;
5398 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5399 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5401 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5403 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
5404 hstmt = _account_prepare_query(query);
5406 if( _account_db_err_code() == SQLITE_PERM ){
5407 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5408 return ACCOUNT_ERROR_PERMISSION_DENIED;
5411 _account_query_bind_text(hstmt, binding_count++, app_id);
5413 rc = _account_query_step(hstmt);
5414 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5416 *account_type_record = create_empty_account_type_instance();
5418 while (rc == SQLITE_ROW) {
5419 _account_type_convert_column_to_account_type(hstmt, *account_type_record);
5420 rc = _account_query_step(hstmt);
5423 rc = _account_query_finalize(hstmt);
5424 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5425 account_type_query_label_by_app_id(_account_get_label_text_cb, app_id, (void*)(*account_type_record));
5426 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, app_id,(void*)(*account_type_record));
5429 error_code = ACCOUNT_ERROR_NONE;
5432 if (hstmt != NULL) {
5433 rc = _account_query_finalize(hstmt);
5434 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5438 pthread_mutex_unlock(&account_mutex);
5439 _INFO("_account_type_query_by_app_id end [%d]", error_code);
5443 int _account_type_query_app_id_exist(const char* app_id)
5445 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5448 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5449 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5451 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5453 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
5454 rc = _account_get_record_count(query);
5456 if( _account_db_err_code() == SQLITE_PERM ){
5457 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5458 return ACCOUNT_ERROR_PERMISSION_DENIED;
5462 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5465 return ACCOUNT_ERROR_NONE;
5468 GSList* _account_type_query_by_provider_feature(const char* key, int *error_code)
5470 *error_code = ACCOUNT_ERROR_NONE;
5471 account_stmt hstmt = NULL;
5472 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5474 GSList *account_type_list = NULL;
5478 ACCOUNT_ERROR("capability_type IS NULL.");
5479 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
5483 if(g_hAccountDB == NULL)
5485 ACCOUNT_ERROR("The database isn't connected.");
5486 *error_code = ACCOUNT_ERROR_DB_NOT_OPENED;
5490 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5492 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
5494 hstmt = _account_prepare_query(query);
5496 if( _account_db_err_code() == SQLITE_PERM )
5498 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5499 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
5503 int binding_count = 1;
5504 _account_query_bind_text(hstmt, binding_count++, key);
5506 rc = _account_query_step(hstmt);
5508 account_type_s *account_type_record = NULL;
5510 if(rc != SQLITE_ROW)
5512 ACCOUNT_ERROR("The record isn't found.");
5513 *error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
5517 while(rc == SQLITE_ROW) {
5518 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
5520 if (account_type_record == NULL) {
5521 ACCOUNT_FATAL("malloc Failed");
5525 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
5526 _account_type_convert_column_to_account_type(hstmt, account_type_record);
5527 account_type_list = g_slist_append(account_type_list, account_type_record);
5528 rc = _account_query_step(hstmt);
5531 rc = _account_query_finalize(hstmt);
5532 if (rc != ACCOUNT_ERROR_NONE )
5534 _account_type_gslist_free(account_type_list);
5535 ACCOUNT_ERROR("finalize error(%s)", rc);
5543 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
5544 account_type_s *account_type = NULL;
5545 account_type = (account_type_s*)iter->data;
5546 account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
5547 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
5550 *error_code = ACCOUNT_ERROR_NONE;
5553 if (hstmt != NULL) {
5554 rc = _account_query_finalize(hstmt);
5555 if (rc != ACCOUNT_ERROR_NONE)
5563 return account_type_list;
5567 GSList* _account_type_query_all(void)
5569 account_stmt hstmt = NULL;
5570 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5572 GSList *account_type_list = NULL;
5574 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
5576 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5578 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
5579 hstmt = _account_prepare_query(query);
5581 rc = _account_query_step(hstmt);
5583 if( _account_db_err_code() == SQLITE_PERM ){
5584 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5588 account_type_s *account_type_record = NULL;
5590 if (rc != SQLITE_ROW)
5592 _INFO("[ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found.");
5596 while(rc == SQLITE_ROW) {
5597 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
5599 if (account_type_record == NULL) {
5600 ACCOUNT_FATAL("malloc Failed");
5604 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
5605 _account_type_convert_column_to_account_type(hstmt, account_type_record);
5606 account_type_list = g_slist_append(account_type_list, account_type_record);
5607 rc = _account_query_step(hstmt);
5610 rc = _account_query_finalize(hstmt);
5611 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, NULL, ("finalize error"));
5616 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
5617 account_type_s *account_type = NULL;
5618 account_type = (account_type_s*)iter->data;
5619 account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
5620 account_type_query_provider_feature_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
5626 rc = _account_query_finalize(hstmt);
5627 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {_account_type_gslist_free(account_type_list);}, NULL, ("finalize error"));
5631 return account_type_list;
5634 // output parameter label must be free
5635 int _account_type_query_label_by_locale(const char* app_id, const char* locale, char **label)
5637 int error_code = ACCOUNT_ERROR_NONE;
5638 account_stmt hstmt = NULL;
5639 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5640 int rc = 0, binding_count = 1;
5641 char* converted_locale = NULL;
5643 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
5644 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5645 ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
5646 ACCOUNT_RETURN_VAL((locale != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null"));
5647 //Making label newly created
5649 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5651 converted_locale = _account_get_text(locale);
5652 gchar** tokens = g_strsplit(converted_locale, "-", 2);
5654 if(tokens != NULL) {
5655 if((char*)(tokens[1]) != NULL) {
5656 char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
5657 if(upper_token != NULL) {
5658 _ACCOUNT_FREE(converted_locale);
5659 converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
5661 _ACCOUNT_FREE(upper_token);
5666 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale);
5667 _ACCOUNT_FREE(converted_locale);
5669 hstmt = _account_prepare_query(query);
5671 if( _account_db_err_code() == SQLITE_PERM ){
5672 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5673 return ACCOUNT_ERROR_PERMISSION_DENIED;
5676 _account_query_bind_text(hstmt, binding_count++, app_id);
5678 rc = _account_query_step(hstmt);
5679 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5681 label_s* label_record = NULL;
5683 while (rc == SQLITE_ROW) {
5684 label_record = (label_s*) malloc(sizeof(label_s));
5686 if (label_record == NULL) {
5687 ACCOUNT_FATAL("malloc Failed");
5691 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
5693 _account_type_convert_column_to_label(hstmt,label_record);
5695 _ACCOUNT_FREE(*label);
5696 //Making label newly created
5697 *label = _account_get_text(label_record->label);
5699 _account_type_free_label_items(label_record);
5700 _ACCOUNT_FREE(label_record);
5702 rc = _account_query_step(hstmt);
5705 rc = _account_query_finalize(hstmt);
5706 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5709 error_code = ACCOUNT_ERROR_NONE;
5712 if (hstmt != NULL) {
5713 rc = _account_query_finalize(hstmt);
5714 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5718 pthread_mutex_unlock(&account_mutex);
5719 _INFO("_account_type_query_label_by_locale() end : error_code = %d", error_code);
5723 static int _account_insert_custom(account_s *account, int account_id)
5725 _INFO("_account_insert_custom start");
5728 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5729 account_stmt hstmt = NULL;
5731 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
5733 if (g_slist_length( account->custom_list)==0) {
5734 ACCOUNT_DEBUG( "_account_insert_custom, no custom data\n");
5735 return ACCOUNT_ERROR_NONE;
5738 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
5740 rc = _account_get_record_count(query);
5742 if( _account_db_err_code() == SQLITE_PERM ){
5743 ACCOUNT_ERROR( "Access failed(%d, %s)", _account_db_err_msg());
5744 return ACCOUNT_ERROR_PERMISSION_DENIED;
5748 ACCOUNT_SLOGE( "_account_insert_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
5749 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5756 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
5759 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5760 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (AccountId, AppId, Key, Value) VALUES "
5761 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
5763 hstmt = _account_prepare_query(query);
5765 if( _account_db_err_code() == SQLITE_PERM ){
5766 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5767 return ACCOUNT_ERROR_PERMISSION_DENIED;
5770 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5772 account_custom_s* custom_data = NULL;
5773 custom_data = (account_custom_s*)iter->data;
5775 ret = _account_query_bind_int(hstmt, count++, account_id);
5776 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
5777 ret = _account_query_bind_text(hstmt, count++, account->package_name);
5778 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5779 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
5780 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5781 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
5782 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5784 rc = _account_query_step(hstmt);
5786 if (rc != SQLITE_DONE) {
5787 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5791 rc = _account_query_finalize(hstmt);
5792 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5797 _INFO("_account_insert_custom end");
5798 return ACCOUNT_ERROR_NONE;
5801 static int _account_update_custom(account_s *account, int account_id)
5804 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5805 account_stmt hstmt = NULL;
5807 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
5809 if (g_slist_length( account->custom_list)==0) {
5810 ACCOUNT_DEBUG( "_account_update_custom, no custom data\n");
5811 return ACCOUNT_ERROR_NONE;
5814 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
5816 rc = _account_get_record_count(query);
5818 if( _account_db_err_code() == SQLITE_PERM ){
5819 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5820 pthread_mutex_unlock(&account_mutex);
5821 return ACCOUNT_ERROR_PERMISSION_DENIED;
5822 } else if( _account_db_err_code() == SQLITE_BUSY ){
5823 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
5824 pthread_mutex_unlock(&account_mutex);
5825 return ACCOUNT_ERROR_DATABASE_BUSY;
5829 ACCOUNT_SLOGE( "_account_update_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
5830 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5833 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5835 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId=? ", ACCOUNT_CUSTOM_TABLE);
5836 hstmt = _account_prepare_query(query);
5838 _account_query_bind_int(hstmt, count++, (int)account_id);
5839 rc = _account_query_step(hstmt);
5841 if (rc == SQLITE_BUSY) {
5842 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5843 return ACCOUNT_ERROR_DATABASE_BUSY;
5844 } else if (rc != SQLITE_DONE) {
5845 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5846 return ACCOUNT_ERROR_DB_FAILED;
5849 rc = _account_query_finalize(hstmt);
5850 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5855 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
5858 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5859 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AccountId, AppId, Key, Value) VALUES "
5860 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
5862 hstmt = _account_prepare_query(query);
5864 if( _account_db_err_code() == SQLITE_PERM ){
5865 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5866 return ACCOUNT_ERROR_PERMISSION_DENIED;
5869 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5871 account_custom_s* custom_data = NULL;
5872 custom_data = (account_custom_s*)iter->data;
5874 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
5875 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
5876 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
5877 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5878 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
5879 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5880 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
5881 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5883 rc = _account_query_step(hstmt);
5885 if (rc != SQLITE_DONE) {
5886 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5890 rc = _account_query_finalize(hstmt);
5891 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5896 return ACCOUNT_ERROR_NONE;
5899 static int _account_query_custom_by_account_id(account_custom_cb callback, int account_id, void *user_data )
5901 int error_code = ACCOUNT_ERROR_NONE;
5902 account_stmt hstmt = NULL;
5903 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5906 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
5907 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
5908 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5910 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5912 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
5913 hstmt = _account_prepare_query(query);
5915 if( _account_db_err_code() == SQLITE_PERM ){
5916 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5917 return ACCOUNT_ERROR_PERMISSION_DENIED;
5920 rc = _account_query_step(hstmt);
5922 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5924 account_custom_s* custom_record = NULL;
5926 while (rc == SQLITE_ROW) {
5927 bool cb_ret = FALSE;
5928 custom_record = (account_custom_s*) malloc(sizeof(account_custom_s));
5930 if (custom_record == NULL) {
5931 ACCOUNT_FATAL("malloc Failed");
5935 ACCOUNT_MEMSET(custom_record, 0x00, sizeof(account_custom_s));
5937 _account_convert_column_to_custom(hstmt, custom_record);
5939 cb_ret = callback(custom_record->key, custom_record->value, user_data);
5941 _account_custom_item_free(custom_record);
5942 _ACCOUNT_FREE(custom_record);
5944 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
5946 rc = _account_query_step(hstmt);
5949 rc = _account_query_finalize(hstmt);
5950 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5953 error_code = ACCOUNT_ERROR_NONE;
5956 if (hstmt != NULL) {
5957 rc = _account_query_finalize(hstmt);
5958 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5962 pthread_mutex_unlock(&account_mutex);