3 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
31 #include <pkgmgr-info.h>
33 #include <tzplatform_config.h>
36 #include <account_ipc_marshal.h>
37 #include <account_free.h>
38 #include <account-private.h>
40 #include <account-error.h>
41 #include "account-server-db.h"
42 #include "account-key-handler.h"
43 #include "account-crypto-service.h"
45 typedef sqlite3_stmt* account_stmt;
47 #define TEST_APP_ID "org.tizen.MyAccountCoreTest"
48 #define EAS_CMDLINE "/usr/bin/eas-engine"
49 #define EMAIL_SERVICE_CMDLINE "/usr/bin/email-service"
50 #define IMS_ENGINE_CMDLINE "/usr/bin/ims-srv"
51 #define IMS_AGENT_CMDLINE "/usr/bin/ims-agent"
52 #define MDM_SERVER_CMDLINE "/usr/bin/mdm-server"
54 #define RCS_APPID "com.samsung.rcs-im"
55 #define IMS_SERVICE_APPID "ims-service"
56 #define ACTIVESYNC_APPID "eas-ui"
57 #define EMAIL_APPID "email-setting-efl"
58 #define SYNCHRONISE_APPID "setting-synchronise-efl"
59 #define DS_AGENT_CMDLINE "/usr/bin/oma-ds-agent"
61 #define FACEBOOK_SDK_APPID "com.samsung.facebook-service"
62 #define FACEBOOK_APPID "com.samsung.facebook"
64 #define EASYSIGNUP_CMDLINE "/usr/bin/esu-agent"
65 #define EASYSIGNUP_APPID "com.samsung.esu-agent"
67 #define ACCESS_TOKEN_ALIAS "access_token"
69 #define ACCOUNT_DB_OPEN_READONLY 0
70 #define ACCOUNT_DB_OPEN_READWRITE 1
74 #define _TIZEN_PUBLIC_
75 #ifndef _TIZEN_PUBLIC_
79 static sqlite3* g_hAccountDB = NULL;
80 static sqlite3* g_hAccountDB2 = NULL;
81 static sqlite3* g_hAccountGlobalDB = NULL;
82 static sqlite3* g_hAccountGlobalDB2 = NULL;
83 pthread_mutex_t account_mutex = PTHREAD_MUTEX_INITIALIZER;
84 pthread_mutex_t account_global_mutex = PTHREAD_MUTEX_INITIALIZER;
86 int _account_db_handle_close(sqlite3* hDB);
87 static char *_account_get_text(const char *text_data);
88 static const char *_account_query_table_column_text(account_stmt pStmt, int pos);
89 static int _account_insert_custom(account_s *account, int account_id);
90 static int _account_update_custom(account_s *account, int account_id);
91 static int _account_query_custom_by_account_id(account_custom_cb callback, int account_id, void *user_data );
92 static int _account_type_update_provider_feature(account_type_s *account_type, const char* app_id);
94 int _account_query_capability_by_account_id(capability_cb callback, int account_id, void *user_data );
96 static void _account_insert_delete_update_notification_send(char *noti_name)
99 _ERR("Noti Name is NULL!!!!!!\n");
103 _INFO("noti_type = %s", noti_name);
105 if (vconf_set_str(VCONFKEY_ACCOUNT_MSG_STR, noti_name) != 0) {
106 _ERR("Vconf MSG Str set FAILED !!!!!!\n");;
110 int _account_get_current_appid_cb(const pkgmgrinfo_appinfo_h handle, void *user_data)
114 GSList** appid_list = (GSList**)user_data;
117 pkgmgr_ret = pkgmgrinfo_appinfo_get_appid(handle, &appid);
119 if( pkgmgr_ret != PMINFO_R_OK ){
120 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_appid(%d)", pkgmgr_ret);
123 item = _account_get_text(appid);
124 *appid_list = g_slist_append(*appid_list, item);
129 static inline int __read_proc(const char *path, char *buf, int size)
133 if (buf == NULL || path == NULL) {
134 ACCOUNT_ERROR("path and buffer is mandatory\n");
138 fd = open(path, O_RDONLY);
140 ACCOUNT_ERROR("fd open error(%d)\n", fd);
144 ret = read(fd, buf, size - 1);
146 ACCOUNT_ERROR("fd read error(%d)\n", fd);
157 char *_account_get_proc_cmdline_bypid(int pid)
162 ACCOUNT_SNPRINTF(buf, sizeof(buf), "/proc/%d/cmdline", pid);
163 ret = __read_proc(buf, buf, sizeof(buf));
165 ACCOUNT_DEBUG("No proc directory (%d)\n", pid);
173 static char* _account_get_current_appid(int pid)
175 _INFO("getting caller appid with pid=[%d]", pid);
178 char appid[128]={0,};
179 char* appid_ret = NULL;
181 ret = aul_app_get_appid_bypid(pid, appid, sizeof(appid));
184 ACCOUNT_ERROR("fail to get current appid ret=[%d], appid=%s\n", ret, appid);
189 /* SLP platform core exception */
190 if(strlen(appid) == 0){
192 char* cmdline = NULL;
193 cmdline = _account_get_proc_cmdline_bypid(pid);
194 ACCOUNT_SLOGD("cmdline (%s)!!!!!!\n", cmdline);
195 if(!g_strcmp0(cmdline, EAS_CMDLINE)) {
196 appid_ret = _account_get_text(ACTIVESYNC_APPID);
197 _ACCOUNT_FREE(cmdline);
199 } else if (!g_strcmp0(cmdline, EMAIL_SERVICE_CMDLINE) || !g_strcmp0(cmdline, MDM_SERVER_CMDLINE)) {
200 appid_ret = _account_get_text(EMAIL_APPID);
201 _ACCOUNT_FREE(cmdline);
203 } else if (!g_strcmp0(cmdline, IMS_ENGINE_CMDLINE) || !g_strcmp0(cmdline, IMS_AGENT_CMDLINE)) {
204 if(_account_type_query_app_id_exist_from_all_db(RCS_APPID) == ACCOUNT_ERROR_NONE) {
205 appid_ret = _account_get_text(RCS_APPID);
206 } else if(_account_type_query_app_id_exist_from_all_db(IMS_SERVICE_APPID) == ACCOUNT_ERROR_NONE) {
207 appid_ret = _account_get_text(IMS_SERVICE_APPID);
209 appid_ret = _account_get_text(RCS_APPID);
211 _ACCOUNT_FREE(cmdline);
213 } else if (!g_strcmp0(cmdline, DS_AGENT_CMDLINE)) {
214 appid_ret = _account_get_text(SYNCHRONISE_APPID);
215 _ACCOUNT_FREE(cmdline);
217 } else if (!g_strcmp0(cmdline, EASYSIGNUP_CMDLINE)) {
218 appid_ret = _account_get_text(EASYSIGNUP_APPID);
219 _ACCOUNT_FREE(cmdline);
222 ACCOUNT_DEBUG("No app id\n");
223 _ACCOUNT_FREE(cmdline);
229 /* temporary exception */
230 if(!g_strcmp0(appid, "com.samsung.gallery")){
231 appid_ret = _account_get_text("com.samsung.facebook");
232 } else if(!g_strcmp0(appid, FACEBOOK_SDK_APPID)){
233 appid_ret = _account_get_text(FACEBOOK_APPID);
235 appid_ret = _account_get_text(appid);
241 static const char *_account_db_err_msg_from_global_db()
243 return sqlite3_errmsg(g_hAccountGlobalDB);
246 static int _account_db_err_code_from_global_db()
248 return sqlite3_errcode(g_hAccountGlobalDB);
251 static int _account_get_record_count_from_global_db(const char *query)
253 _INFO("_account_get_record_count_in_global_db");
257 account_stmt pStmt = NULL;
260 _ERR("NULL query\n");
261 return ACCOUNT_ERROR_QUERY_SYNTAX_ERROR;
264 if(!g_hAccountGlobalDB) {
265 _ERR("DB is not opened\n");
266 return ACCOUNT_ERROR_DB_NOT_OPENED;
269 rc = sqlite3_prepare_v2(g_hAccountGlobalDB, query, strlen(query), &pStmt, NULL);
271 if (SQLITE_BUSY == rc){
272 _ERR("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg_from_global_db());
273 sqlite3_finalize(pStmt);
274 return ACCOUNT_ERROR_DATABASE_BUSY;
275 } else if (SQLITE_OK != rc) {
276 _ERR("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg_from_global_db());
277 sqlite3_finalize(pStmt);
278 return ACCOUNT_ERROR_DB_FAILED;
281 rc = sqlite3_step(pStmt);
282 if (SQLITE_BUSY == rc) {
283 _ERR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg_from_global_db());
284 sqlite3_finalize(pStmt);
285 return ACCOUNT_ERROR_DATABASE_BUSY;
286 } else if (SQLITE_ROW != rc) {
287 _ERR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg_from_global_db());
288 sqlite3_finalize(pStmt);
289 return ACCOUNT_ERROR_DB_FAILED;
292 ncount = sqlite3_column_int(pStmt, 0);
294 _INFO("account record count [%d]", ncount);
295 sqlite3_finalize(pStmt);
300 static int _account_execute_query_from_global_db(const char *query)
303 char* pszErrorMsg = NULL;
306 ACCOUNT_ERROR("NULL query\n");
307 return ACCOUNT_ERROR_QUERY_SYNTAX_ERROR;
310 if(!g_hAccountGlobalDB){
311 ACCOUNT_ERROR("Global DB is not opened\n");
312 return ACCOUNT_ERROR_DB_NOT_OPENED;
315 rc = sqlite3_exec(g_hAccountGlobalDB, query, NULL, NULL, &pszErrorMsg);
316 if (SQLITE_OK != rc) {
317 ACCOUNT_ERROR("sqlite3_exec rc(%d) query(%s) failed(%s).", rc, query, pszErrorMsg);
318 sqlite3_free(pszErrorMsg);
325 static int _account_begin_transaction_from_global_db(void)
327 ACCOUNT_DEBUG("_account_begin_transaction start");
330 ret = _account_execute_query_from_global_db("BEGIN IMMEDIATE TRANSACTION");
332 if (ret == SQLITE_BUSY){
333 ACCOUNT_ERROR(" sqlite3 busy = %d", ret);
334 return ACCOUNT_ERROR_DATABASE_BUSY;
335 } else if(ret != SQLITE_OK) {
336 ACCOUNT_ERROR("_account_svc_begin_transaction_in_global_db fail :: %d", ret);
337 return ACCOUNT_ERROR_DB_FAILED;
340 ACCOUNT_DEBUG("_account_begin_transaction_in_global_db end");
341 return ACCOUNT_ERROR_NONE;
344 static int _account_end_transaction_from_global_db(bool is_success)
346 ACCOUNT_DEBUG("_account_end_transaction_in_global_db start");
350 if (is_success == true) {
351 ret = _account_execute_query_from_global_db("COMMIT TRANSACTION");
352 ACCOUNT_DEBUG("_account_end_transaction_in_global_db COMMIT");
354 ret = _account_execute_query_from_global_db("ROLLBACK TRANSACTION");
355 ACCOUNT_DEBUG("_account_end_transaction ROLLBACK");
358 if(ret == SQLITE_PERM) {
359 ACCOUNT_ERROR("Account permission denied :: %d", ret);
360 return ACCOUNT_ERROR_PERMISSION_DENIED;
363 if (ret == SQLITE_BUSY){
364 ACCOUNT_DEBUG(" sqlite3 busy = %d", ret);
365 return ACCOUNT_ERROR_DATABASE_BUSY;
368 if (ret != SQLITE_OK) {
369 ACCOUNT_ERROR("_account_svc_end_transaction_in_global_db fail :: %d", ret);
370 return ACCOUNT_ERROR_DB_FAILED;
373 ACCOUNT_DEBUG("_account_end_transaction_in_global_db end");
374 return ACCOUNT_ERROR_NONE;
377 int _account_type_query_app_id_exist_from_global_db(const char *app_id)
379 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
382 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
383 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
385 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
387 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
389 pthread_mutex_lock(&account_global_mutex);
391 ret_transaction = _account_begin_transaction_from_global_db();
393 if(_account_db_err_code() == SQLITE_PERM){
394 pthread_mutex_unlock(&account_global_mutex);
395 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg_global());
396 return ACCOUNT_ERROR_PERMISSION_DENIED;
399 if (ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY) {
400 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
401 pthread_mutex_unlock(&account_global_mutex);
402 return ACCOUNT_ERROR_DATABASE_BUSY;
403 }else if (ret_transaction != ACCOUNT_ERROR_NONE) {
404 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
405 pthread_mutex_unlock(&account_global_mutex);
406 return ret_transaction;
409 rc = _account_get_record_count_from_global_db(query);
411 if( _account_db_err_code_from_global_db() == SQLITE_PERM ){
412 _ERR( "Global DB access failed(%s)", _account_db_err_msg_from_global_db());
414 return ACCOUNT_ERROR_PERMISSION_DENIED;
418 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
421 return ACCOUNT_ERROR_NONE;
424 int _account_global_db_open(void)
428 char account_db_path[256] = {0, };
430 _INFO( "start _account_global_db_open()");
432 ACCOUNT_MEMSET(account_db_path, 0x00, sizeof(account_db_path));
433 ACCOUNT_GET_GLOBAL_DB_PATH(account_db_path, sizeof(account_db_path));
435 if( g_hAccountGlobalDB ) {
436 _ERR( "Account database is using in another app. %x", g_hAccountDB );
437 return ACCOUNT_ERROR_DATABASE_BUSY;
440 ret = _account_db_handle_close(g_hAccountGlobalDB2);
441 if( ret != ACCOUNT_ERROR_NONE )
442 ACCOUNT_DEBUG( "db_util_close(g_hAccountGlobalDB2) fail ret = %d", ret);
444 ACCOUNT_DEBUG( "before db_util_open()");
445 // if(mode == ACCOUNT_DB_OPEN_READWRITE)
446 // rc = db_util_open(account_db_path, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD);
447 // else if(mode == ACCOUNT_DB_OPEN_READONLY)
448 rc = db_util_open_with_options(account_db_path, &g_hAccountGlobalDB, SQLITE_OPEN_READONLY, NULL);
450 // return ACCOUNT_ERROR_DB_NOT_OPENED;
451 ACCOUNT_DEBUG( "after db_util_open() sqlite_rc = %d", rc);
453 if( rc == SQLITE_PERM || _account_db_err_code_from_global_db() == SQLITE_PERM ) {
454 ACCOUNT_ERROR( "Account permission denied");
455 return ACCOUNT_ERROR_PERMISSION_DENIED;
458 if( rc == SQLITE_BUSY ) {
459 ACCOUNT_ERROR( "busy handler fail.");
460 return ACCOUNT_ERROR_DATABASE_BUSY;
463 if( rc != SQLITE_OK ) {
464 ACCOUNT_ERROR( "The database isn't connected." );
465 return ACCOUNT_ERROR_DB_NOT_OPENED;
468 _INFO( "end _account_global_db_open()");
469 return ACCOUNT_ERROR_NONE;
472 int _account_global_db_close(void)
474 ACCOUNT_DEBUG( "start account_global_db_close()");
477 ret = _account_db_handle_close(g_hAccountGlobalDB2);
478 if( ret != ACCOUNT_ERROR_NONE )
479 ACCOUNT_DEBUG( "db_util_close(g_hAccountGlobalDB2) fail ret = %d", ret);
481 ret = _account_db_handle_close(g_hAccountGlobalDB);
482 if( ret != ACCOUNT_ERROR_NONE )
484 ACCOUNT_ERROR( "db_util_close(g_hAccountGlobalDB) fail ret = %d", ret);
485 g_hAccountGlobalDB2 = g_hAccountGlobalDB;
487 g_hAccountGlobalDB = NULL;
492 static int _account_check_account_type_with_appid_group(int uid, const char* appid, char** verified_appid)
494 int error_code = ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
495 pkgmgrinfo_appinfo_h ahandle=NULL;
496 pkgmgrinfo_pkginfo_h phandle=NULL;
497 char* package_id = NULL;
498 GSList* appid_list = NULL;
502 ACCOUNT_ERROR("input param is null\n");
503 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
507 ACCOUNT_ERROR("output param is null\n");
508 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
511 if(!strcmp(appid, "com.samsung.setting")){
512 ACCOUNT_DEBUG("Setting exception\n");
513 *verified_appid = _account_get_text("com.samsung.setting");
514 return ACCOUNT_ERROR_NONE;
517 if(!strcmp(appid, "com.samsung.samsung-account-front")){
518 ACCOUNT_DEBUG("Setting exception\n");
519 *verified_appid = _account_get_text("com.samsung.samsung-account-front");
520 return ACCOUNT_ERROR_NONE;
523 if(!strcmp(appid, IMS_SERVICE_APPID) || !strcmp(appid, RCS_APPID)){
524 ACCOUNT_DEBUG("ims service exception\n");
525 *verified_appid = _account_get_text(appid);
526 return ACCOUNT_ERROR_NONE;
529 if(!strcmp(appid, EASYSIGNUP_APPID)){
530 ACCOUNT_DEBUG("easysignup exception\n");
531 *verified_appid = _account_get_text(appid);
532 return ACCOUNT_ERROR_NONE;
534 /* Get app id family which is stored in account database */
537 if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
538 pkgmgr_ret = pkgmgrinfo_appinfo_get_appinfo(appid, &ahandle);
540 pkgmgr_ret = pkgmgrinfo_appinfo_get_usr_appinfo(appid, uid, &ahandle);
542 if( pkgmgr_ret != PMINFO_R_OK ){
543 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_appinfo(%d)", pkgmgr_ret);
546 pkgmgr_ret = pkgmgrinfo_appinfo_get_pkgid(ahandle, &package_id);
547 if( pkgmgr_ret != PMINFO_R_OK ){
548 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_pkgid(%d)", pkgmgr_ret);
551 if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
552 pkgmgr_ret = pkgmgrinfo_pkginfo_get_pkginfo(package_id, &phandle);
554 pkgmgr_ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(package_id, uid, &phandle);
556 if( pkgmgr_ret != PMINFO_R_OK ){
557 ACCOUNT_DEBUG("pkgmgrinfo_pkginfo_get_pkginfo(%d)", pkgmgr_ret);
560 if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
561 pkgmgr_ret = pkgmgrinfo_appinfo_get_list(phandle, PMINFO_ALL_APP, _account_get_current_appid_cb, (void *)&appid_list);
563 pkgmgr_ret = pkgmgrinfo_appinfo_get_usr_list(phandle, PMINFO_ALL_APP, _account_get_current_appid_cb, (void *)&appid_list, uid);
565 if( pkgmgr_ret != PMINFO_R_OK ){
566 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_list(%d)", pkgmgr_ret);
569 /* Compare current app id with the stored app id family */
570 for(iter=appid_list;iter!=NULL;iter=g_slist_next(iter)){
571 char* tmp = (char*)iter->data;
573 if(_account_type_query_app_id_exist_from_all_db(tmp) == ACCOUNT_ERROR_NONE) {
574 *verified_appid = _account_get_text(tmp);
575 error_code = ACCOUNT_ERROR_NONE;
579 ACCOUNT_SLOGD("not matched owner group app id(%s), current appid(%s)\n", tmp, appid);
585 g_slist_free(appid_list);
586 pkgmgr_ret = pkgmgrinfo_pkginfo_destroy_pkginfo(phandle);
587 if( pkgmgr_ret != PMINFO_R_OK ){
588 ACCOUNT_DEBUG("pkgmgrinfo_pkginfo_destroy_pkginfo(%d)", pkgmgr_ret);
591 pkgmgr_ret = pkgmgrinfo_appinfo_destroy_appinfo(ahandle);
592 if( pkgmgr_ret != PMINFO_R_OK ){
593 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_destroy_appinfo(%d)", pkgmgr_ret);
599 static int _account_check_appid_group_with_package_name(int uid, const char* appid, char* package_name)
601 int error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
602 pkgmgrinfo_appinfo_h ahandle=NULL;
603 pkgmgrinfo_pkginfo_h phandle=NULL;
604 char* package_id = NULL;
605 GSList* appid_list = NULL;
609 ACCOUNT_ERROR("input param -appid is null\n");
610 return ACCOUNT_ERROR_INVALID_PARAMETER;
614 ACCOUNT_ERROR("input param - package name is null\n");
615 return ACCOUNT_ERROR_INVALID_PARAMETER;
618 /* ims-service Exception */
619 if ( strcmp(appid, IMS_SERVICE_APPID) == 0 && strcmp(package_name, IMS_SERVICE_APPID) == 0 ) {
620 ACCOUNT_DEBUG("ims exception."); // TODO: NEED TO REMOVE, debug log.
621 return ACCOUNT_ERROR_NONE;
624 /* easysignup Exception */
625 if ( strcmp(appid, EASYSIGNUP_APPID) == 0 && strcmp(package_name, EASYSIGNUP_APPID) == 0 ) {
626 ACCOUNT_DEBUG("easysignup exception."); // TODO: NEED TO REMOVE, debug log.
627 return ACCOUNT_ERROR_NONE;
629 /* Get app id family which is stored in account database */
631 if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
632 pkgmgr_ret = pkgmgrinfo_appinfo_get_appinfo(appid, &ahandle);
634 pkgmgr_ret = pkgmgrinfo_appinfo_get_usr_appinfo(appid, uid, &ahandle);
636 if( pkgmgr_ret != PMINFO_R_OK ){
637 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_appinfo(%d)", pkgmgr_ret);
640 pkgmgr_ret = pkgmgrinfo_appinfo_get_pkgid(ahandle, &package_id);
641 if( pkgmgr_ret != PMINFO_R_OK ){
642 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_pkgid(%d)", pkgmgr_ret);
645 if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
646 pkgmgr_ret = pkgmgrinfo_pkginfo_get_pkginfo(package_id, &phandle);
648 pkgmgr_ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(package_id, uid, &phandle);
650 if( pkgmgr_ret != PMINFO_R_OK ){
651 ACCOUNT_DEBUG("pkgmgrinfo_pkginfo_get_pkginfo(%d)", pkgmgr_ret);
654 if (uid == OWNER_ROOT || uid == GLOBAL_USER) {
655 pkgmgr_ret = pkgmgrinfo_appinfo_get_list(phandle, PMINFO_ALL_APP, _account_get_current_appid_cb, (void *)&appid_list);
657 pkgmgr_ret = pkgmgrinfo_appinfo_get_usr_list(phandle, PMINFO_ALL_APP, _account_get_current_appid_cb, (void *)&appid_list, uid);
659 if( pkgmgr_ret != PMINFO_R_OK ){
660 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_get_list(%d)", pkgmgr_ret);
663 /* Compare current app id with the stored app id family */
664 for(iter=appid_list;iter!=NULL;iter=g_slist_next(iter)){
665 char* tmp = (char*)iter->data;
667 //ACCOUNT_ERROR("tmp(%s)package_name(%s)\n\n", tmp, package_name); // TODO: NEED TO REMOVE, debug log.
668 if( strcmp(tmp, package_name) == 0) {
669 error_code = ACCOUNT_ERROR_NONE;
672 } else if ( strcmp(tmp, "com.samsung.samsung-account-front") == 0 &&
673 strcmp(package_name, "com.samsung.samsungaccount") == 0 ) {
674 /* Samung Account Exception */
675 error_code = ACCOUNT_ERROR_NONE;
679 ACCOUNT_SLOGD("not matched owner group app id(%s), current appid(%s)\n", tmp, appid);
685 g_slist_free(appid_list);
686 pkgmgr_ret = pkgmgrinfo_pkginfo_destroy_pkginfo(phandle);
687 if( pkgmgr_ret != PMINFO_R_OK ){
688 ACCOUNT_DEBUG("pkgmgrinfo_pkginfo_destroy_pkginfo(%d)", pkgmgr_ret);
691 pkgmgr_ret = pkgmgrinfo_appinfo_destroy_appinfo(ahandle);
692 if( pkgmgr_ret != PMINFO_R_OK ){
693 ACCOUNT_DEBUG("pkgmgrinfo_appinfo_destroy_appinfo(%d)", pkgmgr_ret);
699 static int __encrypt_data(unsigned char *data, const int data_len, char **pp_encrypted_data)
702 int enc_data_len = 0;
704 const char *alias = ACCESS_TOKEN_ALIAS;
705 unsigned char *key = NULL;
707 _INFO("before account_key_handler_get_account_dek");
709 ret = account_key_handler_get_account_dek(alias, &key, &key_len);
710 if (ret != ACCOUNT_ERROR_NONE) {
712 _ERR("account_key_handler_get_account_dek failed");
714 _INFO("after account_key_handler_get_account_dek");
716 _INFO("before _encrypt_aes_cbc");
718 ret = encrypt_aes_cbc(key, key_len, data, data_len, pp_encrypted_data, &enc_data_len);
719 if (ret != ACCOUNT_ERROR_NONE) {
721 _ERR("encrypt_aes_cbc failed");
724 _INFO("after encrypt_aes_cbc");
726 return ACCOUNT_ERROR_NONE;
730 static int _encrypt_access_token(account_s *account)
733 char *encrypted_token = NULL;
735 if (account->access_token) {
736 _INFO("before __encrypt_data");
737 ret = __encrypt_data((unsigned char *)(account->access_token), strlen(account->access_token), &encrypted_token);
738 _INFO("after _encrypt_data, ret=[%d]", ret);
739 free(account->access_token);
740 account->access_token = NULL;
741 if( ret == ACCOUNT_ERROR_NONE) {
742 account->access_token = encrypted_token;
743 _INFO("after access_token, encrypted_token size=[%d]", strlen(account->access_token));
747 _ERR("_encrypt_data fail");
752 return ACCOUNT_ERROR_NONE;
756 static int __decrypt_data(unsigned char *data, const int data_len, char **pp_decrypted_data)
759 int dec_data_len = 0;
761 const char *alias = ACCESS_TOKEN_ALIAS;
762 unsigned char *key = NULL;
764 _INFO("before account_key_handler_get_account_dek");
765 ret = account_key_handler_get_account_dek(alias, &key, &key_len);
766 if (ret != ACCOUNT_ERROR_NONE) {
768 _ERR("account_key_handler_get_account_dek failed");
771 _INFO("before _decrypt_aes_cbc");
772 decrypt_aes_cbc(key, key_len, data, data_len, pp_decrypted_data, &dec_data_len);
773 if (ret != ACCOUNT_ERROR_NONE) {
775 _ERR("decrypt_aes_cbc failed");
777 _INFO("after decrypt_aes_cbc, dec_data = %s", *pp_decrypted_data);
779 return ACCOUNT_ERROR_NONE;
782 static int _decrypt_access_token(account_s *account)
785 char *decrypted_token = NULL;
787 if (account->access_token) {
788 ret = __decrypt_data((unsigned char *)account->access_token, strlen(account->access_token), &decrypted_token);
789 free(account->access_token);
790 account->access_token = NULL;
791 if( ret == ACCOUNT_ERROR_NONE)
792 account->access_token = decrypted_token;
795 _ERR("_decrypt_access_token fail");
799 _INFO("_decrypt_access_token end");
801 return ACCOUNT_ERROR_NONE;
804 static int _remove_sensitive_info_from_non_owning_account(int caller_pid, account_s *account)
809 return ACCOUNT_ERROR_INVALID_PARAMETER;
812 if (account->package_name)
814 char *caller_package_name = _account_get_current_appid(caller_pid);
815 if (caller_package_name == NULL)
817 _ERR("Could not get caller app id, so removing sensitive info from account id [%d]", account->id);
818 return ACCOUNT_ERROR_INVALID_PARAMETER;
821 if (g_strcmp0(caller_package_name, account->package_name) != 0)
823 // packages dont match, so remove sensitive info
824 _INFO("Removing sensitive info from account id [%d]", account->id);
825 free (account->access_token);
826 account->access_token = NULL;
829 int ret = _decrypt_access_token(account);
830 if (ret != ACCOUNT_ERROR_NONE)
832 _ERR("_decrypt_access_token error");
836 _ACCOUNT_FREE(caller_package_name);
837 return ACCOUNT_ERROR_NONE;
839 return ACCOUNT_ERROR_INVALID_PARAMETER;
842 static int _remove_sensitive_info_from_non_owning_account_list(int caller_pid, GList *account_list)
844 int return_code = ACCOUNT_ERROR_NONE;
846 if (account_list == NULL)
849 return ACCOUNT_ERROR_INVALID_PARAMETER;
852 GList *list_iter = NULL;
853 for (list_iter = account_list; list_iter != NULL; list_iter = g_list_next(list_iter))
855 account_s *account = (account_s *) list_iter->data;
856 int ret = _remove_sensitive_info_from_non_owning_account(caller_pid, account);
857 if( ret != ACCOUNT_ERROR_NONE)
863 static int _remove_sensitive_info_from_non_owning_account_slist(int caller_pid, GSList *account_list)
865 int return_code = ACCOUNT_ERROR_NONE;
867 if (account_list == NULL)
870 return ACCOUNT_ERROR_INVALID_PARAMETER;
873 GSList *list_iter = NULL;
874 for (list_iter = account_list; list_iter != NULL; list_iter = g_slist_next(list_iter))
876 account_s *account = (account_s *) list_iter->data;
877 int ret = _remove_sensitive_info_from_non_owning_account(caller_pid, account);
878 if( ret != ACCOUNT_ERROR_NONE)
884 static const char *_account_db_err_msg()
886 return sqlite3_errmsg(g_hAccountDB);
889 static int _account_db_err_code()
891 return sqlite3_errcode(g_hAccountDB);
894 static int _account_get_record_count(char* query)
896 _INFO("_account_get_record_count");
900 account_stmt pStmt = NULL;
903 _ERR("NULL query\n");
904 return ACCOUNT_ERROR_QUERY_SYNTAX_ERROR;
908 _ERR("DB is not opened\n");
909 return ACCOUNT_ERROR_DB_NOT_OPENED;
912 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
914 if (SQLITE_BUSY == rc){
915 _ERR("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg());
916 sqlite3_finalize(pStmt);
917 return ACCOUNT_ERROR_DATABASE_BUSY;
918 } else if (SQLITE_OK != rc) {
919 _ERR("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg());
920 sqlite3_finalize(pStmt);
921 return ACCOUNT_ERROR_DB_FAILED;
924 rc = sqlite3_step(pStmt);
925 if (SQLITE_BUSY == rc) {
926 _ERR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg());
927 sqlite3_finalize(pStmt);
928 return ACCOUNT_ERROR_DATABASE_BUSY;
929 } else if (SQLITE_ROW != rc) {
930 _ERR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg());
931 sqlite3_finalize(pStmt);
932 return ACCOUNT_ERROR_DB_FAILED;
935 ncount = sqlite3_column_int(pStmt, 0);
937 _INFO("account record count [%d]", ncount);
938 sqlite3_finalize(pStmt);
943 static int _account_execute_query(const char *query)
946 char* pszErrorMsg = NULL;
949 ACCOUNT_ERROR("NULL query\n");
950 return ACCOUNT_ERROR_QUERY_SYNTAX_ERROR;
954 ACCOUNT_ERROR("DB is not opened\n");
955 return ACCOUNT_ERROR_DB_NOT_OPENED;
958 rc = sqlite3_exec(g_hAccountDB, query, NULL, NULL, &pszErrorMsg);
959 if (SQLITE_OK != rc) {
960 ACCOUNT_ERROR("sqlite3_exec rc(%d) query(%s) failed(%s).", rc, query, pszErrorMsg);
961 sqlite3_free(pszErrorMsg);
967 static int _account_begin_transaction(void)
969 ACCOUNT_DEBUG("_account_begin_transaction start");
972 ret = _account_execute_query("BEGIN IMMEDIATE TRANSACTION");
974 if (ret == SQLITE_BUSY){
975 ACCOUNT_ERROR(" sqlite3 busy = %d", ret);
976 return ACCOUNT_ERROR_DATABASE_BUSY;
977 } else if(ret != SQLITE_OK) {
978 ACCOUNT_ERROR("_account_svc_begin_transaction fail :: %d", ret);
979 return ACCOUNT_ERROR_DB_FAILED;
982 ACCOUNT_DEBUG("_account_begin_transaction end");
983 return ACCOUNT_ERROR_NONE;
986 static int _account_end_transaction(bool is_success)
988 ACCOUNT_DEBUG("_account_end_transaction start");
992 if (is_success == true) {
993 ret = _account_execute_query("COMMIT TRANSACTION");
994 ACCOUNT_DEBUG("_account_end_transaction COMMIT");
996 ret = _account_execute_query("ROLLBACK TRANSACTION");
997 ACCOUNT_DEBUG("_account_end_transaction ROLLBACK");
1000 if(ret == SQLITE_PERM){
1001 ACCOUNT_ERROR("Account permission denied :: %d", ret);
1002 return ACCOUNT_ERROR_PERMISSION_DENIED;
1005 if (ret == SQLITE_BUSY){
1006 ACCOUNT_DEBUG(" sqlite3 busy = %d", ret);
1007 return ACCOUNT_ERROR_DATABASE_BUSY;
1010 if (ret != SQLITE_OK) {
1011 ACCOUNT_ERROR("_account_svc_end_transaction fail :: %d", ret);
1012 return ACCOUNT_ERROR_DB_FAILED;
1015 ACCOUNT_DEBUG("_account_end_transaction end");
1016 return ACCOUNT_ERROR_NONE;
1019 static bool _account_check_add_more_account(const char* app_id)
1021 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1024 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
1025 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1027 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1029 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s' and MultipleAccountSupport = 1", ACCOUNT_TYPE_TABLE, app_id);
1030 rc = _account_get_record_count(query);
1032 /* multiple account support case */
1034 ACCOUNT_SLOGD("app id (%s) supports multiple account. rc(%d)\n", app_id, rc);
1038 /* multiple account not support case */
1039 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1040 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE package_name = '%s'", ACCOUNT_TABLE, app_id);
1041 rc = _account_get_record_count(query);
1044 ACCOUNT_SLOGD("app id (%s) supports single account. and there is no account of the app id\n", app_id);
1051 //TODO: Need to enable creating db on the first connect for
1052 //a) multi-user cases
1053 //b) to ensure db exist in every connect call
1055 static int _account_create_all_tables(void)
1058 int error_code = ACCOUNT_ERROR_NONE;
1059 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1061 _INFO("create all table - BEGIN");
1062 ACCOUNT_MEMSET(query, 0, sizeof(query));
1064 /*Create the account table*/
1065 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_TABLE);
1066 rc = _account_get_record_count(query);
1068 rc = _account_execute_query(ACCOUNT_SCHEMA);
1069 if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY;
1070 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()));
1074 /*Create capability table*/
1075 ACCOUNT_MEMSET(query, 0, sizeof(query));
1076 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", CAPABILITY_TABLE);
1077 rc = _account_get_record_count(query);
1079 rc = _account_execute_query(CAPABILITY_SCHEMA);
1080 if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY;
1081 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()));
1084 /* Create account custom table */
1085 ACCOUNT_MEMSET(query, 0, sizeof(query));
1086 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_CUSTOM_TABLE);
1087 rc = _account_get_record_count(query);
1089 rc = _account_execute_query(ACCOUNT_CUSTOM_SCHEMA);
1090 if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY;
1091 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
1094 /* Create account type table */
1095 ACCOUNT_MEMSET(query, 0, sizeof(query));
1096 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_TYPE_TABLE);
1097 rc = _account_get_record_count(query);
1099 rc = _account_execute_query(ACCOUNT_TYPE_SCHEMA);
1100 if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY;
1101 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()));
1104 /* Create label table */
1105 ACCOUNT_MEMSET(query, 0, sizeof(query));
1106 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", LABEL_TABLE);
1107 rc = _account_get_record_count(query);
1109 rc = _account_execute_query(LABEL_SCHEMA);
1110 if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY;
1111 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()));
1114 /* Create account feature table */
1115 ACCOUNT_MEMSET(query, 0, sizeof(query));
1116 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", PROVIDER_FEATURE_TABLE);
1117 rc = _account_get_record_count(query);
1119 rc = _account_execute_query(PROVIDER_FEATURE_SCHEMA);
1120 if(rc == SQLITE_BUSY) return ACCOUNT_ERROR_DATABASE_BUSY;
1121 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()));
1124 _INFO("create all table - END");
1128 static int _account_check_is_all_table_exists()
1131 char query[ACCOUNT_SQL_LEN_MAX] = {0,};
1132 ACCOUNT_MEMSET(query, 0, sizeof(query));
1134 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s', '%s', '%s', '%s', '%s', '%s')",
1135 ACCOUNT_TABLE, CAPABILITY_TABLE, ACCOUNT_CUSTOM_TABLE, ACCOUNT_TYPE_TABLE, LABEL_TABLE, PROVIDER_FEATURE_TABLE);
1136 rc = _account_get_record_count(query);
1138 if (rc != ACCOUNT_TABLE_TOTAL_COUNT) {
1139 ACCOUNT_ERROR("Table count is not matched rc=%d\n", rc);
1145 int _account_db_handle_close(sqlite3* hDB)
1148 int ret = ACCOUNT_ERROR_NONE;
1151 rc = db_util_close(hDB);
1152 if( rc == SQLITE_OK )
1153 ret = ACCOUNT_ERROR_NONE;
1154 else if( rc == SQLITE_PERM )
1155 ret = ACCOUNT_ERROR_PERMISSION_DENIED;
1156 else if ( rc == SQLITE_BUSY )
1157 ret = ACCOUNT_ERROR_DATABASE_BUSY;
1159 ret = ACCOUNT_ERROR_DB_FAILED;
1164 int _account_db_open(int mode, int pid, int uid)
1168 char account_db_dir[256] = {0, };
1169 char account_db_path[256] = {0, };
1171 _INFO( "start _account_db_open()");
1173 ACCOUNT_MEMSET(account_db_dir, 0x00, sizeof(account_db_dir));
1174 ACCOUNT_MEMSET(account_db_path, 0x00, sizeof(account_db_path));
1176 ACCOUNT_GET_USER_DB_PATH(account_db_path, sizeof(account_db_path), uid);
1178 if( g_hAccountDB ) {
1179 _ERR( "Account database is using in another app. %x", g_hAccountDB );
1180 return ACCOUNT_ERROR_DATABASE_BUSY;
1183 ret = _account_db_handle_close(g_hAccountDB2);
1184 if( ret != ACCOUNT_ERROR_NONE )
1185 ACCOUNT_DEBUG( "db_util_close(g_hAccountDB2) fail ret = %d", ret);
1187 ACCOUNT_GET_USER_DB_DIR(account_db_dir, sizeof(account_db_dir), uid);
1188 if (-1 == access (account_db_dir, F_OK)) {
1189 mkdir(account_db_dir, 644);
1192 ACCOUNT_DEBUG( "before db_util_open()");
1193 // if(mode == ACCOUNT_DB_OPEN_READWRITE)
1194 rc = db_util_open(account_db_path, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD);
1195 // else if(mode == ACCOUNT_DB_OPEN_READONLY)
1196 // rc = db_util_open_with_options(account_db_path, &g_hAccountDB, SQLITE_OPEN_READONLY, NULL);
1198 // return ACCOUNT_ERROR_DB_NOT_OPENED;
1199 ACCOUNT_DEBUG( "after db_util_open() sqlite_rc = %d", rc);
1201 if( rc == SQLITE_PERM || _account_db_err_code() == SQLITE_PERM ) {
1202 ACCOUNT_ERROR( "Account permission denied");
1203 return ACCOUNT_ERROR_PERMISSION_DENIED;
1206 if( rc == SQLITE_BUSY ) {
1207 ACCOUNT_ERROR( "busy handler fail.");
1208 return ACCOUNT_ERROR_DATABASE_BUSY;
1211 if( rc != SQLITE_OK ) {
1212 ACCOUNT_ERROR( "The database isn't connected." );
1213 return ACCOUNT_ERROR_DB_NOT_OPENED;
1216 rc = _account_check_is_all_table_exists();
1219 _ERR("_account_check_is_all_table_exists rc=[%d]", rc);
1221 } else if (rc == ACCOUNT_TABLE_TOTAL_COUNT) {
1224 int ret = _account_create_all_tables();
1225 if (ret != ACCOUNT_ERROR_NONE) {
1226 _ERR("_account_create_all_tables fail ret=[%d]", ret);
1231 _INFO( "end _account_db_open()");
1232 return ACCOUNT_ERROR_NONE;
1235 int _account_db_close(void)
1237 ACCOUNT_DEBUG( "start db_util_close()");
1240 ret = _account_db_handle_close(g_hAccountDB2);
1241 if( ret != ACCOUNT_ERROR_NONE )
1242 ACCOUNT_DEBUG( "db_util_close(g_hAccountDB2) fail ret = %d", ret);
1244 ret = _account_db_handle_close(g_hAccountDB);
1245 if( ret != ACCOUNT_ERROR_NONE )
1247 ACCOUNT_ERROR( "db_util_close(g_hAccountDB) fail ret = %d", ret);
1248 g_hAccountDB2 = g_hAccountDB;
1250 g_hAccountDB = NULL;
1255 static int _account_check_duplicated(account_s *data, const char* verified_appid)
1257 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1261 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1263 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')"
1264 , ACCOUNT_TABLE, verified_appid, data->user_name, data->display_name, data->email_address);
1266 count = _account_get_record_count(query);
1269 return ACCOUNT_ERROR_NONE;
1272 //check whether duplicated account or not.
1273 //1. check user_name
1274 //2. check display_name
1275 //3. check email_address
1276 GList* account_list_temp = _account_query_account_by_package_name(getpid(), verified_appid, &ret);
1277 if (account_list_temp == NULL)
1279 _ERR("_account_query_account_by_package_name returned NULL");
1280 return ACCOUNT_ERROR_DB_FAILED;
1283 if( _account_db_err_code() == SQLITE_PERM ){
1284 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1285 return ACCOUNT_ERROR_PERMISSION_DENIED;
1288 if(ret != ACCOUNT_ERROR_NONE){
1292 account_list_temp = g_list_first(account_list_temp);
1293 _INFO("account_list_temp length=[%d]",g_list_length(account_list_temp));
1296 for (iter = account_list_temp; iter != NULL; iter = g_list_next(iter))
1298 _INFO("iterating account_list_temp");
1299 account_s *account = NULL;
1300 _INFO("Before iter->data");
1301 account = (account_s*)iter->data;
1302 _INFO("After iter->data");
1303 if (account != NULL)
1305 if(account->user_name!=NULL && data->user_name!=NULL && strcmp(account->user_name, data->user_name)==0)
1307 _INFO("duplicated account(s) exist!, same user_name=%s", data->user_name);
1308 return ACCOUNT_ERROR_DUPLICATED;
1310 //when user_name is not NULL and display_name is same.
1311 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)
1313 _INFO("duplicated account(s) exist!, same display_name=%s", data->display_name);
1314 return ACCOUNT_ERROR_DUPLICATED;
1316 //when user_name and display_name are not NULL and email_address is same.
1317 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)
1319 _INFO("duplicated account(s) exist!, same email_address=%s", data->email_address);
1320 return ACCOUNT_ERROR_DUPLICATED;
1325 return ACCOUNT_ERROR_NONE;
1328 static int _account_get_next_sequence(const char *pszName)
1331 account_stmt pStmt = NULL;
1333 char szQuery[ACCOUNT_SQL_LEN_MAX] = {0,};
1335 ACCOUNT_MEMSET(szQuery, 0x00, sizeof(szQuery));
1336 ACCOUNT_SNPRINTF(szQuery, sizeof(szQuery), "SELECT max(seq) FROM %s where name = '%s' ", ACCOUNT_SQLITE_SEQ, pszName);
1337 rc = sqlite3_prepare_v2(g_hAccountDB, szQuery, strlen(szQuery), &pStmt, NULL);
1338 if (SQLITE_OK != rc) {
1339 ACCOUNT_SLOGE("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg());
1340 sqlite3_finalize(pStmt);
1341 return ACCOUNT_ERROR_DB_FAILED;
1344 rc = sqlite3_step(pStmt);
1345 max_seq = sqlite3_column_int(pStmt, 0);
1348 /*Finalize Statement*/
1349 rc = sqlite3_finalize(pStmt);
1355 static account_stmt _account_prepare_query(char *query)
1358 account_stmt pStmt = NULL;
1360 ACCOUNT_RETURN_VAL((query != NULL), {}, NULL, ("query is NULL"));
1362 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
1364 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, NULL, ("sqlite3_prepare_v2(%s) failed(%s).", query, _account_db_err_msg()));
1369 static account_stmt _account_prepare_query_from_global_db(char *query)
1372 account_stmt pStmt = NULL;
1374 ACCOUNT_RETURN_VAL((query != NULL), {}, NULL, ("query is NULL"));
1376 rc = sqlite3_prepare_v2(g_hAccountGlobalDB, query, strlen(query), &pStmt, NULL);
1378 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, NULL, ("sqlite3_prepare_v2(%s) failed(%s).", query, _account_db_err_msg_from_global_db()));
1383 static int _account_query_bind_int(account_stmt pStmt, int pos, int num)
1386 ACCOUNT_ERROR("statement is null");
1391 ACCOUNT_ERROR("invalid pos");
1395 return sqlite3_bind_int(pStmt, pos, num);
1398 static int _account_query_bind_text(account_stmt pStmt, int pos, const char *str)
1400 _INFO("_account_query_bind_text");
1404 _ERR("statement is null");
1410 _INFO("sqlite3_bind_text");
1411 return sqlite3_bind_text(pStmt, pos, (const char*)str, strlen(str), SQLITE_STATIC);
1415 _INFO("sqlite3_bind_null");
1416 return sqlite3_bind_null(pStmt, pos);
1420 static int _account_convert_account_to_sql(account_s *account, account_stmt hstmt, char *sql_value)
1426 /*Caution : Keep insert query orders.*/
1429 _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1430 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], user_name=%s", account->id, account->user_name);
1432 /* 2. email address*/
1433 _account_query_bind_text(hstmt, count++, (char*)account->email_address);
1434 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], email_address=%s", account->id, account->email_address);
1436 /* 3. display name*/
1437 _account_query_bind_text(hstmt, count++, (char*)account->display_name);
1438 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], display_name=%s", account->id, account->display_name);
1441 _account_query_bind_text(hstmt, count++, (char*)account->icon_path);
1442 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], icon_path=%s", account->id, account->icon_path);
1445 _account_query_bind_text(hstmt, count++, (char*)account->source);
1446 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], source=%s", account->id, account->source);
1448 /* 6. package name*/
1449 _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1450 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], package_name=%s", account->id, account->package_name);
1452 /* 7. access token*/
1453 _account_query_bind_text(hstmt, count++, (char*)account->access_token);
1454 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], access_token=%s", account->id, account->access_token);
1457 _account_query_bind_text(hstmt, count++, (char*)account->domain_name);
1458 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], domain_name=%s", account->id, account->domain_name);
1461 _account_query_bind_int(hstmt, count++, account->auth_type);
1462 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], auth_type=%d", account->id, account->auth_type);
1465 _account_query_bind_int(hstmt, count++, account->secret);
1466 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], secret=%d", account->id, account->secret);
1468 /* 11. sync_support */
1469 _account_query_bind_int(hstmt, count++, account->sync_support);
1470 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], sync_support=%d", account->id, account->sync_support);
1475 for(i=0; i< USER_TXT_CNT; i++)
1476 _account_query_bind_text(hstmt, count++, (char*)account->user_data_txt[i]);
1478 /* 13. user integer */
1479 for(i=0; i< USER_INT_CNT; i++)
1481 _account_query_bind_int(hstmt, count++, account->user_data_int[i]);
1482 _INFO("convert user_data_int : marshal_user_int data_int[%d]=%d", i, account->user_data_int[i]);
1490 static int _account_query_finalize(account_stmt pStmt)
1495 ACCOUNT_ERROR( "pStmt is NULL");
1496 return ACCOUNT_ERROR_INVALID_PARAMETER;
1499 rc = sqlite3_finalize(pStmt);
1500 if (rc == SQLITE_BUSY){
1501 ACCOUNT_ERROR(" sqlite3 busy = %d", rc);
1502 return ACCOUNT_ERROR_DATABASE_BUSY;
1503 } else if (rc != SQLITE_OK) {
1504 ACCOUNT_ERROR( "sqlite3_finalize fail, rc : %d, db_error : %s\n", rc, _account_db_err_msg());
1505 return ACCOUNT_ERROR_DB_FAILED;
1508 return ACCOUNT_ERROR_NONE;
1511 static int _account_query_finalize_from_global_db(account_stmt pStmt)
1516 ACCOUNT_ERROR( "pStmt is NULL");
1517 return ACCOUNT_ERROR_INVALID_PARAMETER;
1520 rc = sqlite3_finalize(pStmt);
1521 if (rc == SQLITE_BUSY){
1522 ACCOUNT_ERROR(" sqlite3 busy = %d", rc);
1523 return ACCOUNT_ERROR_DATABASE_BUSY;
1524 } else if (rc != SQLITE_OK) {
1525 ACCOUNT_ERROR( "sqlite3_finalize fail, rc : %d, db_error : %s\n", rc, _account_db_err_msg_from_global_db());
1526 return ACCOUNT_ERROR_DB_FAILED;
1529 return ACCOUNT_ERROR_NONE;
1532 static int _account_query_step(account_stmt pStmt)
1535 ACCOUNT_ERROR( "pStmt is NULL");
1539 return sqlite3_step(pStmt);
1542 static int _account_execute_insert_query(account_s *account)
1544 _INFO("_account_execute_insert_query start");
1547 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1548 int error_code = ACCOUNT_ERROR_NONE;
1549 account_stmt hstmt = NULL;
1551 /* check whether app id exist in account type db */
1553 if (!account->user_name && !account->display_name && !account->email_address) {
1555 ACCOUNT_ERROR("Mandetory fields is NULL. At least one field is required among username, display name, email address\n");
1556 return ACCOUNT_ERROR_INVALID_PARAMETER;
1560 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1561 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (user_name, email_address , display_name , icon_path , source , package_name , "
1562 "access_token , domain_name , auth_type , secret , sync_support , txt_custom0, txt_custom1, txt_custom2, txt_custom3, txt_custom4, "
1563 "int_custom0, int_custom1, int_custom2, int_custom3, int_custom4, txt_custom0 ) values " // to do urusa
1564 "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", ACCOUNT_TABLE);
1566 hstmt = _account_prepare_query(query);
1567 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
1570 _account_convert_account_to_sql(account, hstmt, query);
1573 rc = _account_query_step(hstmt);
1574 if (rc != SQLITE_DONE) {
1576 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1578 if( _account_db_err_code() == SQLITE_PERM )
1579 error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
1581 error_code = ACCOUNT_ERROR_DB_FAILED;
1585 rc = _account_query_finalize(hstmt);
1586 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1589 _INFO("_account_execute_insert_query end");
1593 static int _account_insert_capability(account_s *account, int account_id)
1595 _INFO("_account_insert_capability start");
1597 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1598 account_stmt hstmt = NULL;
1600 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1602 if (g_slist_length( account->capablity_list)==0) {
1603 ACCOUNT_DEBUG( "_account_insert_capability, no capability\n");
1604 return ACCOUNT_ERROR_NONE;
1607 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
1609 _INFO("_account_insert_capability _account_get_record_count [%s]", query);
1610 rc = _account_get_record_count(query);
1612 if( _account_db_err_code() == SQLITE_PERM ){
1613 _ERR( "Access failed(%s)", _account_db_err_msg());
1614 return ACCOUNT_ERROR_PERMISSION_DENIED;
1617 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1624 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1628 account_capability_s* cap_data = NULL;
1629 cap_data = (account_capability_s*)iter->data;
1631 _INFO("cap_data->type = %s, cap_data->value = %d \n", cap_data->type, cap_data->value);
1633 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1634 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
1635 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
1636 hstmt = _account_prepare_query(query);
1638 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
1640 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
1641 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1642 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
1643 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1644 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1645 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1646 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1647 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1648 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
1649 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1651 rc = _account_query_step(hstmt);
1652 _INFO("_account_insert_capability _account_query_step[%d]", rc);
1654 if (rc != SQLITE_DONE) {
1655 _ERR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1659 rc = _account_query_finalize(hstmt);
1660 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1665 _INFO("_account_insert_capability end");
1666 return ACCOUNT_ERROR_NONE;
1669 static int _account_update_capability(account_s *account, int account_id)
1672 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1673 account_stmt hstmt = NULL;
1675 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1677 if (g_slist_length( account->capablity_list)==0) {
1678 ACCOUNT_ERROR( "_account_update_capability, no capability\n");
1679 return ACCOUNT_ERROR_NONE;
1682 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
1684 rc = _account_get_record_count(query);
1687 ACCOUNT_SLOGI( "_account_update_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
1688 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1691 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1693 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id=? ", CAPABILITY_TABLE);
1694 hstmt = _account_prepare_query(query);
1696 _account_query_bind_int(hstmt, count++, (int)account_id);
1697 rc = _account_query_step(hstmt);
1699 if (rc != SQLITE_DONE) {
1700 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1701 return ACCOUNT_ERROR_DB_FAILED;
1703 rc = _account_query_finalize(hstmt);
1704 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1709 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1712 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1713 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
1714 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
1716 hstmt = _account_prepare_query(query);
1718 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
1720 account_capability_s* cap_data = NULL;
1721 cap_data = (account_capability_s*)iter->data;
1723 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
1724 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1725 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
1726 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1727 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1728 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1729 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1730 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1731 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
1732 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1734 rc = _account_query_step(hstmt);
1736 if (rc != SQLITE_DONE) {
1737 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1741 rc = _account_query_finalize(hstmt);
1742 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1747 return ACCOUNT_ERROR_NONE;
1750 static int _account_update_capability_by_user_name(account_s *account, const char *user_name, const char *package_name )
1753 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1754 account_stmt hstmt = NULL;
1756 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1758 if (g_slist_length( account->capablity_list)==0) {
1759 ACCOUNT_ERROR( "_account_update_capability_by_user_name, no capability\n");
1760 return ACCOUNT_ERROR_NONE;
1763 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where package_name= '%s' and user_name='%s'", ACCOUNT_TABLE, package_name, user_name);
1765 rc = _account_get_record_count(query);
1768 ACCOUNT_SLOGI( "_account_update_capability_by_user_name : related account item is not existed rc=%d , %s ", rc, _account_db_err_msg());
1769 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1772 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1774 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name=? and user_name=? ", CAPABILITY_TABLE);
1775 hstmt = _account_prepare_query(query);
1777 _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1778 _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1779 rc = _account_query_step(hstmt);
1780 if (rc != SQLITE_DONE) {
1781 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1782 return ACCOUNT_ERROR_DB_FAILED;
1785 rc = _account_query_finalize(hstmt);
1786 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1791 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1794 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1795 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
1796 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
1798 hstmt = _account_prepare_query(query);
1800 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
1802 account_capability_s* cap_data = NULL;
1803 cap_data = (account_capability_s*)iter->data;
1805 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
1806 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1807 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
1808 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1809 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1810 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1811 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1812 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
1813 ret = _account_query_bind_int(hstmt, count++, (int)account->id);
1814 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
1816 rc = _account_query_step(hstmt);
1818 if (rc != SQLITE_DONE) {
1819 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1823 rc = _account_query_finalize(hstmt);
1824 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1829 return ACCOUNT_ERROR_NONE;
1832 static int _account_query_table_column_int(account_stmt pStmt, int pos)
1835 ACCOUNT_ERROR("statement is null");
1840 ACCOUNT_ERROR("invalid pos");
1844 return sqlite3_column_int(pStmt, pos);
1847 static const char *_account_query_table_column_text(account_stmt pStmt, int pos)
1850 ACCOUNT_ERROR("statement is null");
1855 ACCOUNT_ERROR("invalid pos");
1859 return (const char*)sqlite3_column_text(pStmt, pos);
1862 static void _account_db_data_to_text(const char *textbuf, char **output)
1864 if (textbuf && strlen(textbuf)>0) {
1869 *output = strdup(textbuf);
1873 static void _account_convert_column_to_account(account_stmt hstmt, account_s *account_record)
1875 const char *textbuf = NULL;
1877 account_record->id = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_ID);
1878 ACCOUNT_DEBUG("account_record->id =[%d]", account_record->id);
1880 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_NAME);
1881 _account_db_data_to_text(textbuf, &(account_record->user_name));
1883 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_EMAIL_ADDRESS);
1884 _account_db_data_to_text(textbuf, &(account_record->email_address));
1886 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DISPLAY_NAME);
1887 _account_db_data_to_text(textbuf, &(account_record->display_name));
1889 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ICON_PATH);
1890 _account_db_data_to_text(textbuf, &(account_record->icon_path));
1892 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_SOURCE);
1893 _account_db_data_to_text(textbuf, &(account_record->source));
1895 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_PACKAGE_NAME);
1896 _account_db_data_to_text(textbuf, &(account_record->package_name));
1898 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ACCESS_TOKEN);
1899 _account_db_data_to_text(textbuf, &(account_record->access_token));
1901 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DOMAIN_NAME);
1902 _account_db_data_to_text(textbuf, &(account_record->domain_name));
1904 account_record->auth_type = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_AUTH_TYPE);
1906 account_record->secret = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SECRET);
1908 account_record->sync_support = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SYNC_SUPPORT);
1910 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_0);
1911 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[0]));
1913 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_1);
1914 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[1]));
1916 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_2);
1917 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[2]));
1919 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_3);
1920 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[3]));
1922 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_4);
1923 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[4]));
1925 account_record->user_data_int[0] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_0);
1926 account_record->user_data_int[1] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_1);
1927 account_record->user_data_int[2] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_2);
1928 account_record->user_data_int[3] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_3);
1929 account_record->user_data_int[4] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_4);
1932 static void _account_convert_column_to_capability(account_stmt hstmt, account_capability_s *capability_record)
1934 const char *textbuf = NULL;
1936 _INFO("start _account_convert_column_to_capability()");
1937 capability_record->id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ID);
1939 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_KEY);
1940 _account_db_data_to_text(textbuf, &(capability_record->type));
1942 capability_record->value = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_VALUE);
1944 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_PACKAGE_NAME);
1945 _account_db_data_to_text(textbuf, &(capability_record->package_name));
1947 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_USER_NAME);
1948 _account_db_data_to_text(textbuf, &(capability_record->user_name));
1950 capability_record->account_id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ACCOUNT_ID);
1951 _INFO("type = %s, value = %d", capability_record->type, capability_record->value);
1952 _INFO("end _account_convert_column_to_capability()");
1955 static void _account_convert_column_to_custom(account_stmt hstmt, account_custom_s *custom_record)
1957 _INFO("start _account_convert_column_to_custom()");
1958 const char *textbuf = NULL;
1960 custom_record->account_id = _account_query_table_column_int(hstmt, ACCOUNT_CUSTOM_FIELD_ACCOUNT_ID);
1962 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_APP_ID);
1963 _account_db_data_to_text(textbuf, &(custom_record->app_id));
1965 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_KEY);
1966 _account_db_data_to_text(textbuf, &(custom_record->key));
1968 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_VALUE);
1969 _account_db_data_to_text(textbuf, &(custom_record->value));
1970 _INFO("key = %s, value = %s", custom_record->key, custom_record->value);
1971 _INFO("end _account_convert_column_to_custom()");
1974 bool _account_get_capability_text_cb(const char* capability_type, account_capability_state_e capability_value, void *user_data)
1976 account_s *data = (account_s*)user_data;
1978 account_capability_s *cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
1980 if (cap_data == NULL)
1982 ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
1984 cap_data->type = _account_get_text(capability_type);
1985 cap_data->value = capability_value;
1986 _INFO("cap_data->type = %s, cap_data->value = %d", cap_data->type, cap_data->value);
1988 data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data);
1994 bool _account_get_custom_text_cb(char* key, char* value, void *user_data)
1996 account_s *data = (account_s*)user_data;
1998 account_custom_s *custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
2000 if (custom_data == NULL) {
2001 ACCOUNT_DEBUG("_account_get_custom_text_cb :: malloc fail\n");
2004 ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
2006 custom_data->account_id = data->id;
2007 custom_data->app_id = _account_get_text(data->package_name);
2008 custom_data->key = _account_get_text(key);
2009 custom_data->value = _account_get_text(value);
2010 _INFO("custom_data->key = %s, custom_data->value = %s", custom_data->key, custom_data->value);
2012 data->custom_list = g_slist_append(data->custom_list, (gpointer)custom_data);
2018 static char *_account_get_text(const char *text_data)
2020 char *text_value = NULL;
2022 if (text_data != NULL) {
2023 text_value = strdup(text_data);
2028 static int _account_compare_old_record_by_user_name(account_s *new_account, const char* user_name, const char* package_name)
2030 int error_code = ACCOUNT_ERROR_NONE;
2031 account_stmt hstmt = NULL;
2032 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2034 account_s *old_account = NULL;
2036 ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2037 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
2038 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
2039 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2041 old_account = (account_s*)calloc(1, sizeof(account_s));
2043 ACCOUNT_FATAL("Memory alloc fail\n");
2044 return ACCOUNT_ERROR_OUT_OF_MEMORY;
2047 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2049 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = '%s' and package_name='%s'", ACCOUNT_TABLE, user_name, package_name);
2050 hstmt = _account_prepare_query(query);
2052 rc = _account_query_step(hstmt);
2053 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2055 while (rc == SQLITE_ROW) {
2056 _account_convert_column_to_account(hstmt, old_account);
2057 rc = _account_query_step(hstmt);
2060 rc = _account_query_finalize(hstmt);
2061 ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2065 error_code = _account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account);
2066 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
2069 error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account);
2070 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
2073 new_account->id = old_account->id;
2076 if(!new_account->user_name) {
2077 if(old_account->user_name)
2078 new_account->user_name = _account_get_text(old_account->user_name);
2082 if(!new_account->display_name) {
2083 if(old_account->display_name)
2084 new_account->display_name = _account_get_text(old_account->display_name);
2088 if(!new_account->email_address) {
2089 if(old_account->email_address)
2090 new_account->email_address = _account_get_text(old_account->email_address);
2094 if(!new_account->domain_name) {
2095 if(old_account->domain_name)
2096 new_account->domain_name = _account_get_text(old_account->domain_name);
2100 if(!new_account->icon_path) {
2101 if(old_account->icon_path)
2102 new_account->icon_path = _account_get_text(old_account->icon_path);
2106 if(!new_account->source) {
2107 if(old_account->source)
2108 new_account->source = _account_get_text(old_account->source);
2111 _ACCOUNT_FREE(new_account->package_name);
2112 new_account->package_name = _account_get_text(old_account->package_name);
2115 if(!new_account->access_token) {
2116 if(old_account->access_token)
2117 new_account->access_token = _account_get_text(old_account->access_token);
2121 if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) {
2122 new_account->auth_type = old_account->auth_type;
2126 if(new_account->secret== ACCOUNT_SECRECY_INVALID) {
2127 new_account->secret = old_account->secret;
2131 if(new_account->sync_support == ACCOUNT_SYNC_INVALID) {
2132 new_account->sync_support = old_account->sync_support;
2137 for(i=0;i<USER_TXT_CNT;i++) {
2138 if(!new_account->user_data_txt[i]) {
2139 if(old_account->user_data_txt[i])
2140 new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]);
2145 for(i=0;i<USER_INT_CNT;i++) {
2146 if(new_account->user_data_int[i] == 0) {
2147 new_account->user_data_int[i] = old_account->user_data_int[i];
2153 // user custom table
2157 _account_free_account_with_items(old_account);
2160 if (hstmt != NULL) {
2161 rc = _account_query_finalize(hstmt);
2162 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2166 return ACCOUNT_ERROR_NONE;
2171 static int _account_update_account_by_user_name(int pid, int uid, account_s *account, const char *user_name, const char *package_name)
2173 int rc = 0, binding_count = 0, count = 0;
2174 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2175 int error_code = ACCOUNT_ERROR_NONE;
2176 account_stmt hstmt = NULL;
2178 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is NULL.\n"));
2179 ACCOUNT_RETURN_VAL((package_name!= NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is NULL.\n"));
2181 char* current_appid = NULL;
2182 char* verified_appid = NULL;
2184 current_appid = _account_get_current_appid(pid);
2185 error_code = _account_check_account_type_with_appid_group(uid, current_appid, &verified_appid);
2187 _ACCOUNT_FREE(current_appid);
2188 _ACCOUNT_FREE(verified_appid);
2190 if(error_code != ACCOUNT_ERROR_NONE){
2191 ACCOUNT_ERROR("No permission to update\n");
2192 return ACCOUNT_ERROR_PERMISSION_DENIED;
2195 error_code = _encrypt_access_token(account);
2196 if (error_code != ACCOUNT_ERROR_NONE)
2198 _ERR("_encrypt_access_token error");
2202 _account_compare_old_record_by_user_name(account, user_name, package_name);
2204 if( _account_db_err_code() == SQLITE_PERM ){
2205 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2206 return ACCOUNT_ERROR_PERMISSION_DENIED;
2209 if (!account->package_name) {
2210 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
2211 return ACCOUNT_ERROR_INVALID_PARAMETER;
2214 if (!account->user_name && !account->display_name && !account->email_address) {
2215 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
2216 return ACCOUNT_ERROR_INVALID_PARAMETER;
2219 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE user_name='%s' and package_name='%s'"
2220 , ACCOUNT_TABLE, user_name, package_name);
2222 count = _account_get_record_count(query);
2224 if( _account_db_err_code() == SQLITE_PERM ){
2225 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2226 return ACCOUNT_ERROR_PERMISSION_DENIED;
2230 ACCOUNT_SLOGI("_account_update_account_by_user_name : The account not exist!, count = %d, user_name=%s, package_name=%s\n",
2231 count, user_name, package_name);
2232 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2235 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2237 //TODO: Is it required to update id ? As of now I can only think of falied rollback cases (between account and gSSO DB)
2238 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
2239 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
2240 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
2241 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE user_name=? and package_name=? ", ACCOUNT_TABLE);
2243 hstmt = _account_prepare_query(query);
2244 if( _account_db_err_code() == SQLITE_PERM ){
2245 _account_end_transaction(FALSE);
2246 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2247 return ACCOUNT_ERROR_PERMISSION_DENIED;
2249 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
2251 binding_count = _account_convert_account_to_sql(account, hstmt, query);
2253 _account_query_bind_text(hstmt, binding_count++, user_name);
2254 _account_query_bind_text(hstmt, binding_count++, package_name);
2255 rc = _account_query_step(hstmt);
2256 if (rc != SQLITE_DONE) {
2257 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2259 rc = _account_query_finalize(hstmt);
2260 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2263 /*update capability*/
2264 error_code = _account_update_capability_by_user_name(account, user_name, package_name);
2267 error_code = _account_update_custom(account, account->id);
2272 int _account_insert_to_db(account_s* account, int pid, int uid, int *account_id)
2275 int error_code = ACCOUNT_ERROR_NONE;
2276 int ret_transaction = 0;
2278 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2279 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2280 ACCOUNT_RETURN_VAL((account_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID POINTER IS NULL"));
2282 if (!account->user_name && !account->display_name && !account->email_address) {
2283 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
2284 return ACCOUNT_ERROR_INVALID_PARAMETER;
2287 account_s *data = (account_s*)account;
2288 ACCOUNT_SLOGD("(%s)-(%d) account_insert_to_db: begin_transaction.\n", __FUNCTION__, __LINE__);
2290 pthread_mutex_lock(&account_mutex);
2292 /* transaction control required*/
2293 ret_transaction = _account_begin_transaction();
2295 if(_account_db_err_code() == SQLITE_PERM){
2296 pthread_mutex_unlock(&account_mutex);
2297 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2298 return ACCOUNT_ERROR_PERMISSION_DENIED;
2301 if (ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY) {
2302 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
2303 pthread_mutex_unlock(&account_mutex);
2304 return ACCOUNT_ERROR_DATABASE_BUSY;
2305 }else if (ret_transaction != ACCOUNT_ERROR_NONE) {
2306 ACCOUNT_ERROR("account insert:_account_begin_transaction fail %d\n", ret_transaction);
2307 pthread_mutex_unlock(&account_mutex);
2308 return ret_transaction;
2311 *account_id = _account_get_next_sequence(ACCOUNT_TABLE);
2312 data->id = *account_id;
2315 appid = _account_get_current_appid(pid);
2320 // API caller cannot be recognized
2321 ret_transaction = _account_end_transaction(FALSE);
2322 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
2323 pthread_mutex_unlock(&account_mutex);
2324 return ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER;
2328 char* verified_appid = NULL;
2329 error_code = _account_check_account_type_with_appid_group(uid, appid, &verified_appid);//FIX
2330 _ACCOUNT_FREE(appid);
2331 if(error_code != ACCOUNT_ERROR_NONE)
2333 _ERR("error_code = %d", error_code);
2334 ret_transaction = _account_end_transaction(FALSE);
2335 ACCOUNT_ERROR("App id is not registered in account type DB, transaction ret (%x)!!!!\n", ret_transaction);
2336 _ACCOUNT_FREE(verified_appid);
2337 pthread_mutex_unlock(&account_mutex);
2344 error_code = _account_check_duplicated(data, verified_appid);
2345 if (error_code != ACCOUNT_ERROR_NONE) {
2347 ret_transaction = _account_end_transaction(FALSE);
2348 ACCOUNT_DEBUG("_account_check_duplicated(), rollback insert query(%x)!!!!\n", ret_transaction);
2350 pthread_mutex_unlock(&account_mutex);
2353 if(!_account_check_add_more_account(verified_appid)) {
2354 ret_transaction = _account_end_transaction(FALSE);
2355 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
2356 pthread_mutex_unlock(&account_mutex);
2357 _ACCOUNT_FREE(verified_appid);
2358 return ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
2361 _ACCOUNT_FREE(data->package_name);
2362 data->package_name = _account_get_text(verified_appid);
2363 _ACCOUNT_FREE(verified_appid);
2366 if(!_account_check_add_more_account(data->package_name))
2369 ret_transaction = _account_end_transaction(FALSE);
2370 ACCOUNT_ERROR("No more account cannot be added, transaction ret (%x)!!!!\n", ret_transaction);
2371 pthread_mutex_unlock(&account_mutex);
2372 return ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE;
2375 error_code = _encrypt_access_token(data);
2376 if (error_code != ACCOUNT_ERROR_NONE)
2378 _ERR("_encrypt_access_token error");
2382 error_code = _account_execute_insert_query(data);
2384 if (error_code != ACCOUNT_ERROR_NONE)
2387 ret_transaction = _account_end_transaction(FALSE);
2388 ACCOUNT_ERROR("INSERT account fail, rollback insert query(%x)!!!!\n", ret_transaction);
2390 pthread_mutex_unlock(&account_mutex);
2395 error_code = _account_insert_capability(data, *account_id);
2396 if (error_code != ACCOUNT_ERROR_NONE)
2399 ret_transaction = _account_end_transaction(FALSE);
2400 ACCOUNT_ERROR("INSERT capability fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
2402 pthread_mutex_unlock(&account_mutex);
2407 error_code = _account_insert_custom(data, *account_id);
2408 if (error_code != ACCOUNT_ERROR_NONE)
2410 ret_transaction = _account_end_transaction(FALSE);
2411 ACCOUNT_ERROR("INSERT custom fail, rollback insert capability query(%x)!!!!\n", ret_transaction);
2413 pthread_mutex_unlock(&account_mutex);
2419 pthread_mutex_unlock(&account_mutex);
2420 _account_end_transaction(TRUE);
2421 ACCOUNT_SLOGD("(%s)-(%d) account _end_transaction.\n", __FUNCTION__, __LINE__);
2424 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_INSERT, *account_id);
2425 _account_insert_delete_update_notification_send(buf);
2426 _INFO("account _notification_send end.");
2428 return ACCOUNT_ERROR_NONE;
2432 int _account_query_capability_by_account_id(capability_cb callback, int account_id, void *user_data )
2434 int error_code = ACCOUNT_ERROR_NONE;
2435 account_stmt hstmt = NULL;
2436 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2439 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2440 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
2441 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2443 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2445 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2446 hstmt = _account_prepare_query(query);
2448 if( _account_db_err_code() == SQLITE_PERM ){
2449 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2450 return ACCOUNT_ERROR_PERMISSION_DENIED;
2453 rc = _account_query_step(hstmt);
2454 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2456 account_capability_s* capability_record = NULL;
2458 while (rc == SQLITE_ROW) {
2459 bool cb_ret = FALSE;
2460 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
2462 if (capability_record == NULL) {
2463 ACCOUNT_FATAL("malloc Failed");
2467 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
2469 _account_convert_column_to_capability(hstmt, capability_record);
2471 cb_ret = callback(capability_record->type, capability_record->value, user_data);
2473 _account_free_capability_with_items(capability_record);
2475 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
2477 rc = _account_query_step(hstmt);
2480 rc = _account_query_finalize(hstmt);
2481 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2484 error_code = ACCOUNT_ERROR_NONE;
2487 if (hstmt != NULL) {
2488 rc = _account_query_finalize(hstmt);
2489 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2493 pthread_mutex_unlock(&account_mutex);
2497 GSList* _account_get_capability_list_by_account_id(int account_id, int *error_code)
2499 *error_code = ACCOUNT_ERROR_NONE;
2500 account_stmt hstmt = NULL;
2501 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2503 GSList* capability_list = NULL;
2505 ACCOUNT_RETURN_VAL((account_id > 0), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("ACCOUNT INDEX IS LESS THAN 0"));
2506 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
2508 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2510 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2511 hstmt = _account_prepare_query(query);
2513 if( _account_db_err_code() == SQLITE_PERM ){
2514 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2515 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
2519 rc = _account_query_step(hstmt);
2520 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2522 account_capability_s* capability_record = NULL;
2524 while (rc == SQLITE_ROW) {
2525 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
2527 if (capability_record == NULL) {
2528 ACCOUNT_FATAL("malloc Failed");
2532 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
2534 _account_convert_column_to_capability(hstmt, capability_record);
2536 //cb_ret = callback(capability_record->type, capability_record->value, user_data);
2538 //_account_free_capability_items(capability_record);
2539 //_ACCOUNT_FREE(capability_record);
2541 //ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
2543 capability_list = g_slist_append(capability_list, capability_record);
2544 rc = _account_query_step(hstmt);
2547 rc = _account_query_finalize(hstmt);
2548 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
2551 *error_code = ACCOUNT_ERROR_NONE;
2556 rc = _account_query_finalize(hstmt);
2557 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
2561 pthread_mutex_unlock(&account_mutex);
2562 return capability_list;
2565 static int _account_compare_old_record(account_s *new_account, int account_id)
2567 int error_code = ACCOUNT_ERROR_NONE;
2568 account_stmt hstmt = NULL;
2569 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2571 account_s *old_account = NULL;
2573 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2574 ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2575 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2577 old_account = (account_s*)calloc(1, sizeof(account_s));
2578 if (old_account == NULL) {
2579 _ERR("Out of Memory");
2580 return ACCOUNT_ERROR_OUT_OF_MEMORY;
2583 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2585 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2586 hstmt = _account_prepare_query(query);
2588 rc = _account_query_step(hstmt);
2589 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2591 while (rc == SQLITE_ROW) {
2592 _account_convert_column_to_account(hstmt, old_account);
2593 rc = _account_query_step(hstmt);
2596 rc = _account_query_finalize(hstmt);
2597 ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2601 error_code = _account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account);
2602 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
2605 error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account);
2606 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
2610 new_account->id = old_account->id;
2613 if(!new_account->user_name) {
2614 if(old_account->user_name)
2615 new_account->user_name = _account_get_text(old_account->user_name);
2619 if(!new_account->display_name) {
2620 if(old_account->display_name)
2621 new_account->display_name = _account_get_text(old_account->display_name);
2625 if(!new_account->email_address) {
2626 if(old_account->email_address)
2627 new_account->email_address = _account_get_text(old_account->email_address);
2631 if(!new_account->domain_name) {
2632 if(old_account->domain_name)
2633 new_account->domain_name = _account_get_text(old_account->domain_name);
2637 if(!new_account->icon_path) {
2638 if(old_account->icon_path)
2639 new_account->icon_path = _account_get_text(old_account->icon_path);
2643 if(!new_account->source) {
2644 if(old_account->source)
2645 new_account->source = _account_get_text(old_account->source);
2648 _ACCOUNT_FREE(new_account->package_name);
2649 new_account->package_name = _account_get_text(old_account->package_name);
2652 if(!new_account->access_token) {
2653 if(old_account->access_token)
2654 new_account->access_token = _account_get_text(old_account->access_token);
2659 for(i=0;i<USER_TXT_CNT;i++) {
2660 if(!new_account->user_data_txt[i]) {
2661 if(old_account->user_data_txt[i])
2662 new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]);
2667 if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) {
2668 new_account->auth_type = old_account->auth_type;
2672 if(new_account->secret== ACCOUNT_SECRECY_INVALID) {
2673 new_account->secret = old_account->secret;
2677 if(new_account->sync_support == ACCOUNT_SYNC_INVALID) {
2678 new_account->sync_support = old_account->sync_support;
2682 for(i=0;i<USER_INT_CNT;i++) {
2683 if(new_account->user_data_int[i] == 0) {
2684 new_account->user_data_int[i] = old_account->user_data_int[i];
2690 // user custom table
2694 _account_free_account_with_items(old_account);
2696 if (hstmt != NULL) {
2697 rc = _account_query_finalize(hstmt);
2698 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2702 return ACCOUNT_ERROR_NONE;
2705 static int _account_get_package_name_from_account_id(int account_id, char **package_name)
2707 int error_code = ACCOUNT_ERROR_NONE;
2708 account_stmt hstmt = NULL;
2709 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2711 account_s *old_account = NULL;
2713 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2714 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2716 old_account = (account_s*)calloc(1, sizeof(account_s));
2717 if (old_account == NULL) {
2718 _ERR("Out Of memory");
2719 return ACCOUNT_ERROR_OUT_OF_MEMORY;
2722 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2724 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2725 hstmt = _account_prepare_query(query);
2727 rc = _account_query_step(hstmt);
2728 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2730 while (rc == SQLITE_ROW) {
2731 _account_convert_column_to_account(hstmt, old_account);
2732 rc = _account_query_step(hstmt);
2735 rc = _account_query_finalize(hstmt);
2736 ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2739 // get package name.
2740 *package_name = _account_get_text(old_account->package_name);
2745 _account_free_account_with_items(old_account);
2748 if (hstmt != NULL) {
2749 rc = _account_query_finalize(hstmt);
2750 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2758 static int _account_update_account(int pid, int uid, account_s *account, int account_id)
2760 int rc = 0, binding_count =0;
2761 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2762 int error_code = ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0;
2763 account_stmt hstmt = NULL;
2765 if (!account->package_name) {
2766 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
2767 return ACCOUNT_ERROR_INVALID_PARAMETER;
2770 /* Check permission of requested appid */
2771 char* current_appid = NULL;
2772 char *package_name = NULL;
2774 current_appid = _account_get_current_appid(pid);
2775 error_code = _account_get_package_name_from_account_id(account_id, &package_name);
2777 if(error_code != ACCOUNT_ERROR_NONE || package_name == NULL){
2778 ACCOUNT_ERROR("No package name with account_id\n");
2779 _ACCOUNT_FREE(current_appid);
2780 _ACCOUNT_FREE(package_name);
2781 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2784 error_code = _account_check_appid_group_with_package_name(uid, current_appid, package_name);
2785 ACCOUNT_DEBUG( "UPDATE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name); // TODO: remove the log later.
2787 _ACCOUNT_FREE(current_appid);
2788 _ACCOUNT_FREE(package_name);
2790 if(error_code != ACCOUNT_ERROR_NONE){
2791 ACCOUNT_ERROR("No permission to update\n");
2792 return ACCOUNT_ERROR_PERMISSION_DENIED;
2795 error_code = _encrypt_access_token(account);
2796 if (error_code != ACCOUNT_ERROR_NONE)
2798 _ERR("_encrypt_access_token error");
2802 error_code = _account_compare_old_record(account, account_id);
2803 if (error_code != ACCOUNT_ERROR_NONE) {
2804 ACCOUNT_ERROR("_account_compare_old_record fail\n");
2808 if( _account_db_err_code() == SQLITE_PERM ){
2809 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2810 return ACCOUNT_ERROR_PERMISSION_DENIED;
2811 } else if( _account_db_err_code() == SQLITE_BUSY ){
2812 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
2813 return ACCOUNT_ERROR_DATABASE_BUSY;
2816 if (!account->user_name && !account->display_name && !account->email_address) {
2817 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
2818 return ACCOUNT_ERROR_INVALID_PARAMETER;
2821 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2823 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
2825 count = _account_get_record_count(query);
2827 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
2828 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2831 /* transaction control required*/
2832 ret_transaction = _account_begin_transaction();
2833 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
2834 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
2835 pthread_mutex_unlock(&account_mutex);
2836 return ACCOUNT_ERROR_DATABASE_BUSY;
2839 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2840 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
2841 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
2842 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
2843 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
2845 hstmt = _account_prepare_query(query);
2847 if( _account_db_err_code() == SQLITE_PERM ){
2848 ret_transaction = _account_end_transaction(FALSE);
2849 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2850 return ACCOUNT_ERROR_PERMISSION_DENIED;
2853 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)));
2855 binding_count = _account_convert_account_to_sql(account, hstmt, query);
2856 _account_query_bind_int(hstmt, binding_count++, account_id);
2858 rc = _account_query_step(hstmt);
2859 if (rc != SQLITE_DONE) {
2860 ACCOUNT_SLOGE( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2863 rc = _account_query_finalize(hstmt);
2864 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2867 _INFO("update query=%s", query);
2869 /*update capability*/
2870 error_code = _account_update_capability(account, account_id);
2871 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2872 ret_transaction = _account_end_transaction(FALSE);
2873 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2878 error_code = _account_update_custom(account, account_id);
2879 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2880 ret_transaction = _account_end_transaction(FALSE);
2881 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2885 ret_transaction = _account_end_transaction(TRUE);
2887 _INFO("update end");
2892 static int _account_update_account_ex(account_s *account, int account_id)
2894 int rc = 0, binding_count =0;
2895 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2896 int error_code = ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0;
2897 account_stmt hstmt = NULL;
2899 if (!account->package_name) {
2900 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
2901 return ACCOUNT_ERROR_INVALID_PARAMETER;
2904 error_code = _encrypt_access_token(account);
2905 if (error_code != ACCOUNT_ERROR_NONE)
2907 _ERR("_encrypt_access_token error");
2911 error_code = _account_compare_old_record(account, account_id);
2912 if (error_code != ACCOUNT_ERROR_NONE) {
2913 ACCOUNT_ERROR("_account_compare_old_record fail\n");
2917 if( _account_db_err_code() == SQLITE_PERM ){
2918 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2919 return ACCOUNT_ERROR_PERMISSION_DENIED;
2922 if (!account->user_name && !account->display_name && !account->email_address) {
2923 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
2924 return ACCOUNT_ERROR_INVALID_PARAMETER;
2927 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2929 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
2931 count = _account_get_record_count(query);
2933 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
2934 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2937 /* transaction control required*/
2938 ret_transaction = _account_begin_transaction();
2939 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
2940 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
2941 pthread_mutex_unlock(&account_mutex);
2942 return ACCOUNT_ERROR_DATABASE_BUSY;
2945 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2946 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
2947 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
2948 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
2949 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
2951 hstmt = _account_prepare_query(query);
2953 if( _account_db_err_code() == SQLITE_PERM ){
2954 ret_transaction = _account_end_transaction(FALSE);
2955 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2956 return ACCOUNT_ERROR_PERMISSION_DENIED;
2959 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)));
2961 _INFO("account_update_to_db_by_id_ex_p : before convert() : account_id[%d], user_name=%s", account->id, account->user_name);
2962 binding_count = _account_convert_account_to_sql(account, hstmt, query);
2963 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], user_name=%s", account->id, account->user_name);
2964 _INFO("account_update_to_db_by_id_ex_p : before bind()");
2965 rc = _account_query_bind_int(hstmt, binding_count++, account_id);
2966 _INFO("account_update_to_db_by_id_ex_p : after bind() : ret = %d", rc);
2968 rc = _account_query_step(hstmt);
2969 if (rc != SQLITE_DONE) {
2970 ACCOUNT_SLOGE( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2972 _INFO("account_update_to_db_by_id_ex_p : after query_step() : ret = %d", rc);
2974 rc = _account_query_finalize(hstmt);
2975 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2977 _INFO("account_update_to_db_by_id_ex_p : after query_filnalize() : ret = %d", rc);
2979 _INFO("account_update_to_db_by_id_ex_p : before update_capability()");
2980 /*update capability*/
2981 error_code = _account_update_capability(account, account_id);
2982 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2983 ret_transaction = _account_end_transaction(FALSE);
2984 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2987 _INFO("account_update_to_db_by_id_ex_p : after update_capability()");
2989 _INFO("account_update_to_db_by_id_ex_p : before update_custom()");
2991 error_code = _account_update_custom(account, account_id);
2992 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2993 ret_transaction = _account_end_transaction(FALSE);
2994 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2997 _INFO("account_update_to_db_by_id_ex_p : after update_custom()");
2999 ret_transaction = _account_end_transaction(TRUE);
3005 int _account_update_to_db_by_id(int pid, int uid, account_s* account, int account_id)
3007 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
3008 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
3009 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3010 int error_code = ACCOUNT_ERROR_NONE;
3011 account_s* data = (account_s*)account;
3013 pthread_mutex_lock(&account_mutex);
3015 error_code = _account_update_account(pid, uid, data, account_id);
3017 if(error_code != ACCOUNT_ERROR_NONE) {
3018 pthread_mutex_unlock(&account_mutex);
3022 pthread_mutex_unlock(&account_mutex);
3025 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, account_id);
3026 _account_insert_delete_update_notification_send(buf);
3028 return ACCOUNT_ERROR_NONE;
3031 int _account_update_to_db_by_id_ex(account_s* account, int account_id)
3033 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
3034 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
3035 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3036 int error_code = ACCOUNT_ERROR_NONE;
3037 account_s* data = account;
3039 pthread_mutex_lock(&account_mutex);
3041 _INFO("before update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
3042 error_code = _account_update_account_ex(data, account_id);
3043 _INFO("after update_account_ex() : account_id[%d], user_name=%s", account_id, data->user_name);
3045 if(error_code != ACCOUNT_ERROR_NONE) {
3046 pthread_mutex_unlock(&account_mutex);
3050 pthread_mutex_unlock(&account_mutex);
3053 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, account_id);
3054 _account_insert_delete_update_notification_send(buf);
3056 return ACCOUNT_ERROR_NONE;
3060 int _account_update_to_db_by_user_name(int pid, int uid, account_s* account, const char *user_name, const char *package_name)
3062 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
3063 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
3064 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3066 int error_code = ACCOUNT_ERROR_NONE;
3067 account_s *data = (account_s*)account;
3069 pthread_mutex_lock(&account_mutex);
3071 error_code = _account_update_account_by_user_name(pid, uid, data, user_name, package_name);
3073 pthread_mutex_unlock(&account_mutex);
3076 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, data->id);
3077 _account_insert_delete_update_notification_send(buf);
3082 GSList* _account_db_query_all(int pid)
3084 //int error_code = ACCOUNT_ERROR_NONE;
3085 account_stmt hstmt = NULL;
3086 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3088 GSList *account_list = NULL;
3090 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
3092 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3094 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TABLE);
3095 hstmt = _account_prepare_query(query);
3097 if( _account_db_err_code() == SQLITE_PERM ){
3098 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3102 rc = _account_query_step(hstmt);
3104 account_s *account_record = NULL;
3106 if (rc != SQLITE_ROW)
3108 _ERR("The record isn't found");
3112 while(rc == SQLITE_ROW) {
3113 account_record = (account_s*) malloc(sizeof(account_s));
3115 if (account_record == NULL) {
3116 ACCOUNT_FATAL("malloc Failed");
3120 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3121 _account_convert_column_to_account(hstmt, account_record);
3122 account_list = g_slist_append(account_list, account_record);
3123 rc = _account_query_step(hstmt);
3126 rc = _account_query_finalize(hstmt);
3127 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, NULL, ("finalize error"));
3132 for (iter = account_list; iter != NULL; iter = g_slist_next(iter)) {
3133 account_s *account = NULL;
3134 account = (account_s*)iter->data;
3135 _account_query_capability_by_account_id(_account_get_capability_text_cb, account->id, (void*)account);
3136 _account_query_custom_by_account_id(_account_get_custom_text_cb, account->id, (void*)account);
3140 if (hstmt != NULL) {
3141 rc = _account_query_finalize(hstmt);
3142 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {_account_gslist_account_free(account_list);}, NULL, ("finalize error"));
3147 _remove_sensitive_info_from_non_owning_account_slist(pid, account_list);
3149 return account_list;
3152 int _account_update_sync_status_by_id(int uid, int account_db_id, const int sync_status)
3154 int error_code = ACCOUNT_ERROR_NONE;
3155 account_stmt hstmt = NULL;
3156 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3160 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
3161 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3162 if ( (sync_status < 0) || (sync_status > ACCOUNT_SYNC_STATUS_RUNNING)) {
3163 ACCOUNT_SLOGE("(%s)-(%d) sync_status is less than 0 or more than enum max.\n", __FUNCTION__, __LINE__);
3164 return ACCOUNT_ERROR_INVALID_PARAMETER;
3167 pthread_mutex_lock(&account_mutex);
3169 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3171 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_db_id);
3173 rc = _account_get_record_count(query);
3175 if( _account_db_err_code() == SQLITE_PERM ){
3176 pthread_mutex_unlock(&account_mutex);
3177 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3178 return ACCOUNT_ERROR_PERMISSION_DENIED;
3182 ACCOUNT_SLOGE( "account_update_sync_status_by_id : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
3183 pthread_mutex_unlock(&account_mutex);
3184 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3187 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3189 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET sync_support=? WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
3190 hstmt = _account_prepare_query(query);
3192 _account_query_bind_int(hstmt, count, sync_status);
3194 rc = _account_query_step(hstmt);
3196 if( _account_db_err_code() == SQLITE_PERM ){
3197 pthread_mutex_unlock(&account_mutex);
3198 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3199 return ACCOUNT_ERROR_PERMISSION_DENIED;
3202 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_DB_FAILED,
3203 ("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()));
3205 rc = _account_query_finalize(hstmt);
3206 if (rc != ACCOUNT_ERROR_NONE) {
3207 ACCOUNT_ERROR("_account_query_finalize error");
3208 pthread_mutex_unlock(&account_mutex);
3212 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_SYNC_UPDATE, account_db_id);
3213 _account_insert_delete_update_notification_send(buf);
3216 error_code = ACCOUNT_ERROR_NONE;
3219 if (hstmt != NULL) {
3220 rc = _account_query_finalize(hstmt);
3221 pthread_mutex_unlock(&account_mutex);
3222 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3226 pthread_mutex_unlock(&account_mutex);
3230 int _account_query_account_by_account_id(int pid, int account_db_id, account_s *account_record)
3232 _INFO("_account_query_account_by_account_id() start, account_db_id=[%d]", account_db_id);
3234 int error_code = ACCOUNT_ERROR_NONE;
3235 account_stmt hstmt = NULL;
3236 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3239 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
3240 ACCOUNT_RETURN_VAL(account_record != NULL, {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
3241 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3243 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3245 ACCOUNT_DEBUG("starting db operations");
3247 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
3248 hstmt = _account_prepare_query(query);
3249 rc = _account_db_err_code();
3250 _INFO("after _account_prepare_query, rc=[%d]", rc);
3252 if( rc == SQLITE_PERM ){
3253 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3254 return ACCOUNT_ERROR_PERMISSION_DENIED;
3257 ACCOUNT_DEBUG("before _account_query_step");
3258 rc = _account_query_step(hstmt);
3259 ACCOUNT_DEBUG("after _account_query_step returned [%d]", rc);
3260 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3262 while (rc == SQLITE_ROW) {
3263 ACCOUNT_DEBUG("before _account_convert_column_to_account");
3264 _account_convert_column_to_account(hstmt, account_record);
3265 ACCOUNT_DEBUG("after _account_convert_column_to_account");
3266 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]);
3267 rc = _account_query_step(hstmt);
3270 ACCOUNT_DEBUG("account_record->id=[%d]", account_record->id);
3272 rc = _account_query_finalize(hstmt);
3273 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3275 ACCOUNT_DEBUG("before _account_query_capability_by_account_id");
3276 _account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
3277 ACCOUNT_DEBUG("after _account_query_capability_by_account_id");
3279 ACCOUNT_DEBUG("before _account_query_custom_by_account_id");
3280 _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record);
3281 ACCOUNT_DEBUG("after _account_query_custom_by_account_id");
3284 error_code = ACCOUNT_ERROR_NONE;
3287 if (hstmt != NULL) {
3288 rc = _account_query_finalize(hstmt);
3289 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3295 _remove_sensitive_info_from_non_owning_account(pid, account_record);
3297 pthread_mutex_unlock(&account_mutex);
3298 ACCOUNT_DEBUG("_account_query_account_by_account_id end [%d]", error_code);
3302 GList* _account_query_account_by_user_name(int pid, const char *user_name, int *error_code)
3304 *error_code = ACCOUNT_ERROR_NONE;
3305 account_stmt hstmt = NULL;
3306 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3308 account_s *account_head = NULL;
3310 if (user_name == NULL)
3312 _ERR("USER NAME IS NULL");
3313 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
3317 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3319 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ?", ACCOUNT_TABLE);
3321 hstmt = _account_prepare_query(query);
3323 if (_account_db_err_code() == SQLITE_PERM)
3325 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3326 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
3330 int binding_count = 1;
3331 _account_query_bind_text(hstmt, binding_count++, user_name);
3333 rc = _account_query_step(hstmt);
3335 if (rc != SQLITE_ROW)
3337 _ERR("The record isn't found");
3338 *error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
3344 account_head = (account_s*) malloc(sizeof(account_s));
3345 if (account_head == NULL) {
3346 ACCOUNT_FATAL("malloc Failed");
3347 if (hstmt != NULL) {
3348 rc = _account_query_finalize(hstmt);
3350 if (rc != ACCOUNT_ERROR_NONE)
3352 _ERR("finalize error");
3358 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
3361 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3363 while (rc == SQLITE_ROW) {
3364 account_s* account_record = NULL;
3366 account_record = (account_s*) malloc(sizeof(account_s));
3368 if (account_record == NULL) {
3369 ACCOUNT_FATAL("malloc Failed");
3372 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3374 _account_convert_column_to_account(hstmt, account_record);
3376 account_head->account_list = g_list_append(account_head->account_list, account_record);
3378 rc = _account_query_step(hstmt);
3382 rc = _account_query_finalize(hstmt);
3384 if (rc != ACCOUNT_ERROR_NONE)
3386 _ERR("finalize error");
3396 tmp = g_list_length(account_head->account_list);
3398 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3400 account = (account_h)iter->data;
3402 account_s *testaccount = (account_s*)account;
3404 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3405 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3409 *error_code = ACCOUNT_ERROR_NONE;
3412 if (hstmt != NULL) {
3413 rc = _account_query_finalize(hstmt);
3414 if (rc != ACCOUNT_ERROR_NONE)
3416 _ERR("finalize error");
3422 pthread_mutex_unlock(&account_mutex);
3425 _remove_sensitive_info_from_non_owning_account_list(pid, account_head->account_list);
3426 GList* result = account_head->account_list;
3427 _ACCOUNT_FREE(account_head);
3434 _account_query_account_by_capability(int pid, const char* capability_type, const int capability_value, int *error_code)
3436 *error_code = ACCOUNT_ERROR_NONE;
3437 account_stmt hstmt = NULL;
3438 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3441 ACCOUNT_RETURN_VAL((capability_type != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("capability_type IS NULL"));
3443 if ((capability_value < 0) || (capability_value > ACCOUNT_CAPABILITY_ENABLED)) {
3444 ACCOUNT_SLOGE("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
3445 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
3449 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
3451 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3453 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=? AND value=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
3455 hstmt = _account_prepare_query(query);
3457 if( _account_db_err_code() == SQLITE_PERM ){
3458 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3459 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
3463 int binding_count = 1;
3464 _account_query_bind_text(hstmt, binding_count++, capability_type);
3465 _account_query_bind_int(hstmt, binding_count++, capability_value);
3467 rc = _account_query_step(hstmt);
3469 account_s* account_head = NULL;
3471 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3475 account_head = (account_s*) malloc(sizeof(account_s));
3476 if (account_head == NULL) {
3477 ACCOUNT_FATAL("malloc Failed");
3478 if (hstmt != NULL) {
3479 rc = _account_query_finalize(hstmt);
3480 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
3483 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
3486 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3488 while (rc == SQLITE_ROW) {
3489 account_s* account_record = NULL;
3491 account_record = (account_s*) malloc(sizeof(account_s));
3493 if (account_record == NULL) {
3494 ACCOUNT_FATAL("malloc Failed");
3497 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3499 _account_convert_column_to_account(hstmt, account_record);
3501 account_head->account_list = g_list_append(account_head->account_list, account_record);
3503 rc = _account_query_step(hstmt);
3507 rc = _account_query_finalize(hstmt);
3508 ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3514 tmp = g_list_length(account_head->account_list);
3516 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3517 account_h account = NULL;
3518 account = (account_h)iter->data;
3519 account_s* testaccount = (account_s*)account;
3521 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3522 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3527 *error_code = ACCOUNT_ERROR_NONE;
3530 if (hstmt != NULL) {
3531 rc = _account_query_finalize(hstmt);
3532 if ( rc != ACCOUNT_ERROR_NONE ) {
3534 _ERR("finalize error");
3539 if( *error_code != ACCOUNT_ERROR_NONE && account_head ) {
3540 _account_glist_account_free(account_head->account_list);
3541 _ACCOUNT_FREE(account_head);
3542 account_head = NULL;
3545 pthread_mutex_unlock(&account_mutex);
3549 _remove_sensitive_info_from_non_owning_account_list(pid, account_head->account_list);
3550 GList* result = account_head->account_list;
3551 _ACCOUNT_FREE(account_head);
3557 GList* _account_query_account_by_capability_type(int pid, const char* capability_type, int *error_code)
3559 *error_code = ACCOUNT_ERROR_NONE;
3560 account_stmt hstmt = NULL;
3561 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3564 ACCOUNT_RETURN_VAL((capability_type != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("capability_type IS NULL"));
3565 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;},
3566 NULL, ("The database isn't connected."));
3568 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3570 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
3572 hstmt = _account_prepare_query(query);
3574 if( _account_db_err_code() == SQLITE_PERM ){
3575 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3576 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
3580 int binding_count = 1;
3581 _account_query_bind_text(hstmt, binding_count++, capability_type);
3583 rc = _account_query_step(hstmt);
3585 account_s* account_head = NULL;
3587 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3591 account_head = (account_s*) malloc(sizeof(account_s));
3592 if (account_head == NULL) {
3593 ACCOUNT_FATAL("malloc Failed");
3594 if (hstmt != NULL) {
3595 rc = _account_query_finalize(hstmt);
3596 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
3599 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
3602 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3604 while (rc == SQLITE_ROW) {
3605 account_s* account_record = NULL;
3607 account_record = (account_s*) malloc(sizeof(account_s));
3609 if (account_record == NULL) {
3610 ACCOUNT_FATAL("malloc Failed");
3613 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3615 _account_convert_column_to_account(hstmt, account_record);
3617 account_head->account_list = g_list_append(account_head->account_list, account_record);
3619 rc = _account_query_step(hstmt);
3623 rc = _account_query_finalize(hstmt);
3624 ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3630 tmp = g_list_length(account_head->account_list);
3632 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3633 account_s* testaccount = (account_s*)iter->data;
3635 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3636 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3640 *error_code = ACCOUNT_ERROR_NONE;
3645 rc = _account_query_finalize(hstmt);
3646 if (rc != ACCOUNT_ERROR_NONE) {
3648 _ERR("finalize error");
3653 if( (*error_code != ACCOUNT_ERROR_NONE) && account_head ) {
3654 _account_glist_account_free(account_head->account_list);
3655 _ACCOUNT_FREE(account_head);
3656 account_head = NULL;
3659 pthread_mutex_unlock(&account_mutex);
3663 _remove_sensitive_info_from_non_owning_account_list(pid, account_head->account_list);
3664 GList* result = account_head->account_list;
3665 _ACCOUNT_FREE(account_head);
3671 GList* _account_query_account_by_package_name(int pid,const char* package_name, int *error_code)
3673 _INFO("_account_query_account_by_package_name");
3675 *error_code = ACCOUNT_ERROR_NONE;
3676 account_stmt hstmt = NULL;
3677 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3680 ACCOUNT_RETURN_VAL((package_name != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("PACKAGE NAME IS NULL"));
3681 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
3683 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3685 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE package_name=?", ACCOUNT_TABLE);
3687 hstmt = _account_prepare_query(query);
3689 if( _account_db_err_code() == SQLITE_PERM ){
3690 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3691 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
3695 int binding_count = 1;
3696 _account_query_bind_text(hstmt, binding_count++, package_name);
3698 rc = _account_query_step(hstmt);
3700 account_s* account_head = NULL;
3702 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.(%s)\n", package_name));
3706 account_head = (account_s*) malloc(sizeof(account_s));
3707 if (account_head == NULL) {
3708 ACCOUNT_FATAL("malloc Failed");
3709 if (hstmt != NULL) {
3710 rc = _account_query_finalize(hstmt);
3711 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
3714 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
3717 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
3719 while (rc == SQLITE_ROW) {
3720 account_s* account_record = NULL;
3722 account_record = (account_s*) malloc(sizeof(account_s));
3724 if (account_record == NULL) {
3725 ACCOUNT_FATAL("malloc Failed");
3728 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
3730 _account_convert_column_to_account(hstmt, account_record);
3732 _INFO("Adding account_list");
3733 account_head->account_list = g_list_append(account_head->account_list, account_record);
3735 rc = _account_query_step(hstmt);
3739 rc = _account_query_finalize(hstmt);
3740 ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3745 tmp = g_list_length(account_head->account_list);
3747 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
3748 account_s* testaccount = (account_s*)iter->data;
3750 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
3751 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
3754 *error_code = ACCOUNT_ERROR_NONE;
3759 rc = _account_query_finalize(hstmt);
3760 if (rc != ACCOUNT_ERROR_NONE) {
3762 _ERR("finalize error");
3767 pthread_mutex_unlock(&account_mutex);
3769 if( (*error_code != ACCOUNT_ERROR_NONE) && account_head ) {
3770 _account_glist_account_free(account_head->account_list);
3771 _ACCOUNT_FREE(account_head);
3772 account_head = NULL;
3775 if ((*error_code == ACCOUNT_ERROR_NONE) && account_head != NULL)
3777 _INFO("Returning account_list");
3778 _remove_sensitive_info_from_non_owning_account_list(pid,account_head->account_list);
3779 GList* result = account_head->account_list;
3780 _ACCOUNT_FREE(account_head);
3786 int _account_delete(int pid, int uid, int account_id)
3788 int error_code = ACCOUNT_ERROR_NONE;
3789 account_stmt hstmt = NULL;
3790 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3792 int ret_transaction = 0;
3793 bool is_success = FALSE;
3795 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3798 /* Check requested ID to delete */
3799 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id=%d", ACCOUNT_TABLE, account_id);
3801 count = _account_get_record_count(query);
3803 if( _account_db_err_code() == SQLITE_PERM ){
3804 pthread_mutex_unlock(&account_mutex);
3805 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3806 return ACCOUNT_ERROR_PERMISSION_DENIED;
3810 ACCOUNT_ERROR("account id(%d) is not exist. count(%d)\n", account_id, count);
3811 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3814 /* Check permission of requested appid */
3815 char* current_appid = NULL;
3816 char *package_name = NULL;
3818 current_appid = _account_get_current_appid(pid);
3820 error_code = _account_get_package_name_from_account_id(account_id, &package_name);
3822 if(error_code != ACCOUNT_ERROR_NONE){
3823 ACCOUNT_ERROR("No package name with account_id\n");
3824 _ACCOUNT_FREE(current_appid);
3825 _ACCOUNT_FREE(package_name);
3826 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3828 ACCOUNT_DEBUG( "DELETE:account_id[%d],current_appid[%s]package_name[%s]", account_id, current_appid, package_name);
3830 error_code = _account_check_appid_group_with_package_name(uid, current_appid, package_name);
3832 _ACCOUNT_FREE(current_appid);
3833 _ACCOUNT_FREE(package_name);
3835 if(error_code != ACCOUNT_ERROR_NONE){
3836 ACCOUNT_ERROR("No permission to delete\n");
3837 return ACCOUNT_ERROR_PERMISSION_DENIED;
3840 /* transaction control required*/
3841 ret_transaction = _account_begin_transaction();
3843 if( _account_db_err_code() == SQLITE_PERM ){
3844 pthread_mutex_unlock(&account_mutex);
3845 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3846 return ACCOUNT_ERROR_PERMISSION_DENIED;
3849 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
3850 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
3851 pthread_mutex_unlock(&account_mutex);
3852 return ACCOUNT_ERROR_DATABASE_BUSY;
3855 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3856 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3857 pthread_mutex_unlock(&account_mutex);
3858 return ret_transaction;
3861 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3862 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
3864 hstmt = _account_prepare_query(query);
3866 if( _account_db_err_code() == SQLITE_PERM ){
3867 pthread_mutex_unlock(&account_mutex);
3868 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3869 return ACCOUNT_ERROR_PERMISSION_DENIED;
3872 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3873 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3875 rc = _account_query_step(hstmt);
3876 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3878 rc = _account_query_finalize(hstmt);
3880 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3883 ACCOUNT_MEMSET(query, 0, sizeof(query));
3885 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
3887 hstmt = _account_prepare_query(query);
3888 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3889 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3891 rc = _account_query_step(hstmt);
3892 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
3894 rc = _account_query_finalize(hstmt);
3895 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3898 /* delete custom data */
3899 ACCOUNT_MEMSET(query, 0, sizeof(query));
3901 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
3903 hstmt = _account_prepare_query(query);
3905 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3906 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3908 rc = _account_query_step(hstmt);
3909 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
3911 rc = _account_query_finalize(hstmt);
3912 ACCOUNT_CATCH_ERROR(rc == ACCOUNT_ERROR_NONE, {}, rc, ("finalize error", account_id, rc));
3918 if (hstmt != NULL) {
3919 rc = _account_query_finalize(hstmt);
3920 if(rc != ACCOUNT_ERROR_NONE ){
3921 ACCOUNT_ERROR("rc (%d)", rc);
3928 ret_transaction = _account_end_transaction(is_success);
3930 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3931 ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3933 if (is_success == true) {
3935 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id);
3936 _account_insert_delete_update_notification_send(buf);
3940 pthread_mutex_unlock(&account_mutex);
3946 static int _account_query_account_by_username_and_package(const char* username, const char* package_name, account_h *account)
3950 int error_code = ACCOUNT_ERROR_NONE;
3951 account_stmt hstmt = NULL;
3952 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3954 int binding_count = 1;
3956 ACCOUNT_RETURN_VAL((username != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("username IS NULL"));
3957 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name IS NULL"));
3958 ACCOUNT_RETURN_VAL((*account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
3959 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3961 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3963 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
3964 hstmt = _account_prepare_query(query);
3966 if( _account_db_err_code() == SQLITE_PERM ){
3967 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
3968 return ACCOUNT_ERROR_PERMISSION_DENIED;
3971 _account_query_bind_text(hstmt, binding_count++, username);
3972 _account_query_bind_text(hstmt, binding_count++, package_name);
3974 rc = _account_query_step(hstmt);
3975 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3977 account_s *account_record = (account_s *)(*account);
3979 while (rc == SQLITE_ROW) {
3980 _account_convert_column_to_account(hstmt, account_record);
3981 rc = _account_query_step(hstmt);
3984 rc = _account_query_finalize(hstmt);
3985 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3986 _account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
3987 _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record);
3990 error_code = ACCOUNT_ERROR_NONE;
3993 if (hstmt != NULL) {
3994 rc = _account_query_finalize(hstmt);
3995 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
3999 pthread_mutex_unlock(&account_mutex);
4003 int _account_create(account_h *account)
4006 ACCOUNT_SLOGE("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
4007 return ACCOUNT_ERROR_INVALID_PARAMETER;
4010 account_s *data = (account_s*)malloc(sizeof(account_s));
4013 ACCOUNT_FATAL("Memory Allocation Failed");
4014 return ACCOUNT_ERROR_OUT_OF_MEMORY;
4016 ACCOUNT_MEMSET(data, 0, sizeof(account_s));
4018 /*Setting account as visible by default*/
4019 data->secret = ACCOUNT_SECRECY_VISIBLE;
4021 /*Setting account as not supporting sync by default*/
4022 data->sync_support = ACCOUNT_SYNC_NOT_SUPPORT;
4024 *account = (account_h)data;
4026 return ACCOUNT_ERROR_NONE;
4029 int _account_destroy(account_h account)
4031 account_s *data = (account_s*)account;
4033 ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!"));
4035 _account_free_account_with_items(data);
4037 return ACCOUNT_ERROR_NONE;
4040 int _account_get_account_id(account_s* account, int *account_id)
4043 return ACCOUNT_ERROR_INVALID_PARAMETER;
4046 return ACCOUNT_ERROR_INVALID_PARAMETER;
4049 *account_id = account->id;
4051 return ACCOUNT_ERROR_NONE;
4054 int _account_delete_from_db_by_user_name(int pid, int uid, const char *user_name, const char *package_name)
4056 _INFO("[%s][%s]", user_name, package_name);
4058 int error_code = ACCOUNT_ERROR_NONE;
4059 account_stmt hstmt = NULL;
4060 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4062 int ret_transaction = 0;
4063 bool is_success = FALSE;
4064 account_h account = NULL;
4065 int binding_count = 1;
4066 int account_id = -1;
4068 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is null!"));
4069 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
4070 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4072 /* Check permission of requested appid */
4073 char* current_appid = NULL;
4074 char* package_name_temp = NULL;
4076 current_appid = _account_get_current_appid(pid);
4078 package_name_temp = _account_get_text(package_name);
4080 ACCOUNT_DEBUG( "DELETE:user_name[%s],current_appid[%s], package_name[%s]", user_name, current_appid, package_name_temp);
4082 error_code = _account_check_appid_group_with_package_name(uid, current_appid, package_name_temp);
4084 _ACCOUNT_FREE(current_appid);
4085 _ACCOUNT_FREE(package_name_temp);
4087 if(error_code != ACCOUNT_ERROR_NONE){
4088 ACCOUNT_ERROR("No permission to delete\n");
4089 return ACCOUNT_ERROR_PERMISSION_DENIED;
4092 rc = _account_create(&account);
4093 rc = _account_query_account_by_username_and_package(user_name, package_name, &account);
4097 if( _account_db_err_code() == SQLITE_PERM )
4099 _account_destroy(account);
4100 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4101 return ACCOUNT_ERROR_PERMISSION_DENIED;
4105 account_s* account_data = (account_s*)account;
4107 rc = _account_get_account_id(account_data, &account_id);
4109 rc = _account_destroy(account);
4111 /* transaction control required*/
4112 ret_transaction = _account_begin_transaction();
4114 if( _account_db_err_code() == SQLITE_PERM )
4116 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4117 pthread_mutex_unlock(&account_mutex);
4118 return ACCOUNT_ERROR_PERMISSION_DENIED;
4122 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY )
4124 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
4125 pthread_mutex_unlock(&account_mutex);
4126 return ACCOUNT_ERROR_DATABASE_BUSY;
4128 else if (ret_transaction != ACCOUNT_ERROR_NONE) {
4129 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
4130 pthread_mutex_unlock(&account_mutex);
4131 return ret_transaction;
4134 /* delete custom data */
4135 ACCOUNT_MEMSET(query, 0, sizeof(query));
4136 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = ?", ACCOUNT_CUSTOM_TABLE);
4138 hstmt = _account_prepare_query(query);
4140 if( _account_db_err_code() == SQLITE_PERM ){
4141 _account_end_transaction(FALSE);
4142 pthread_mutex_unlock(&account_mutex);
4143 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4144 return ACCOUNT_ERROR_PERMISSION_DENIED;
4147 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4148 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4150 _account_query_bind_int(hstmt, binding_count++, account_id);
4152 rc = _account_query_step(hstmt);
4153 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4155 rc = _account_query_finalize(hstmt);
4156 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4159 /* delete capability */
4160 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", CAPABILITY_TABLE);
4162 hstmt = _account_prepare_query(query);
4164 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4165 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4168 _account_query_bind_text(hstmt, binding_count++, user_name);
4169 _account_query_bind_text(hstmt, binding_count++, package_name);
4171 rc = _account_query_step(hstmt);
4172 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4174 rc = _account_query_finalize(hstmt);
4175 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4178 ACCOUNT_MEMSET(query, 0, sizeof(query));
4181 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
4183 hstmt = _account_prepare_query(query);
4184 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4185 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4189 _account_query_bind_text(hstmt, binding_count++, user_name);
4190 _account_query_bind_text(hstmt, binding_count++, package_name);
4192 rc = _account_query_step(hstmt);
4193 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));
4195 rc = _account_query_finalize(hstmt);
4196 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4202 if (hstmt != NULL) {
4203 rc = _account_query_finalize(hstmt);
4204 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4208 ret_transaction = _account_end_transaction(is_success);
4210 if (ret_transaction != ACCOUNT_ERROR_NONE) {
4211 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
4213 if (is_success == true) {
4215 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_DELETE, account_id);
4216 _account_insert_delete_update_notification_send(buf);
4220 pthread_mutex_unlock(&account_mutex);
4225 int _account_delete_from_db_by_package_name(int pid, int uid, const char *package_name, gboolean permission)
4227 _INFO("_account_delete_from_db_by_package_name");
4228 int error_code = ACCOUNT_ERROR_NONE;
4229 account_stmt hstmt = NULL;
4230 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4232 int ret_transaction = 0;
4233 bool is_success = FALSE;
4234 int binding_count = 1;
4235 GSList *account_id_list = NULL;
4238 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
4239 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4241 // It only needs list of ids, does not need to query sensitive info. So sending 0
4242 GList* account_list_temp = _account_query_account_by_package_name(getpid(), package_name, &ret);
4243 if( _account_db_err_code() == SQLITE_PERM ){
4244 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4245 return ACCOUNT_ERROR_PERMISSION_DENIED;
4248 if(ret != ACCOUNT_ERROR_NONE){
4249 _ERR("_account_query_account_by_package_name failed ret=[%d]", ret);
4253 /* Check permission of requested appid */
4255 char* current_appid = NULL;
4256 char* package_name_temp = NULL;
4258 current_appid = _account_get_current_appid(pid);
4260 package_name_temp = _account_get_text(package_name);
4262 ACCOUNT_DEBUG( "DELETE: current_appid[%s], package_name[%s]", current_appid, package_name_temp);
4264 error_code = _account_check_appid_group_with_package_name(uid, current_appid, package_name_temp);
4266 _ACCOUNT_FREE(current_appid);
4267 _ACCOUNT_FREE(package_name_temp);
4269 if(error_code != ACCOUNT_ERROR_NONE){
4270 ACCOUNT_ERROR("No permission to delete\n");
4271 _account_glist_account_free(account_list_temp);
4272 return ACCOUNT_ERROR_PERMISSION_DENIED;
4276 GList *account_list = g_list_first(account_list_temp);
4277 _INFO("account_list_temp length=[%d]",g_list_length(account_list));
4280 for (iter = account_list; iter != NULL; iter = g_list_next(iter))
4282 _INFO("iterating account_list");
4283 account_s *account = NULL;
4284 _INFO("Before iter->data");
4285 account = (account_s*)iter->data;
4286 _INFO("After iter->data");
4287 if (account != NULL)
4289 char id[256] = {0, };
4291 ACCOUNT_MEMSET(id, 0, 256);
4293 ACCOUNT_SNPRINTF(id, 256, "%d", account->id);
4295 _INFO("Adding account id [%s]", id);
4296 account_id_list = g_slist_append(account_id_list, g_strdup(id));
4300 _account_glist_account_free(account_list_temp);
4302 /* transaction control required*/
4303 ret_transaction = _account_begin_transaction();
4305 if( _account_db_err_code() == SQLITE_PERM ){
4306 pthread_mutex_unlock(&account_mutex);
4307 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4308 return ACCOUNT_ERROR_PERMISSION_DENIED;
4311 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
4312 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
4313 pthread_mutex_unlock(&account_mutex);
4314 return ACCOUNT_ERROR_DATABASE_BUSY;
4315 }else if (ret_transaction != ACCOUNT_ERROR_NONE) {
4316 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
4317 pthread_mutex_unlock(&account_mutex);
4318 return ret_transaction;
4321 /* delete custom table */
4322 ACCOUNT_MEMSET(query, 0, sizeof(query));
4323 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", ACCOUNT_CUSTOM_TABLE);
4325 hstmt = _account_prepare_query(query);
4327 if( _account_db_err_code() == SQLITE_PERM ){
4328 _account_end_transaction(FALSE);
4329 pthread_mutex_unlock(&account_mutex);
4330 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4331 return ACCOUNT_ERROR_PERMISSION_DENIED;
4334 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4335 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4338 _account_query_bind_text(hstmt, binding_count++, package_name);
4340 rc = _account_query_step(hstmt);
4341 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4343 rc = _account_query_finalize(hstmt);
4344 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4347 /* delete capability table */
4348 ACCOUNT_MEMSET(query, 0, sizeof(query));
4349 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", CAPABILITY_TABLE);
4351 hstmt = _account_prepare_query(query);
4353 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4354 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4357 _account_query_bind_text(hstmt, binding_count++, package_name);
4359 rc = _account_query_step(hstmt);
4360 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4362 rc = _account_query_finalize(hstmt);
4363 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4366 /* delete account table */
4367 ACCOUNT_MEMSET(query, 0, sizeof(query));
4369 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", ACCOUNT_TABLE);
4371 hstmt = _account_prepare_query(query);
4372 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
4373 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
4376 _account_query_bind_text(hstmt, binding_count++, package_name);
4378 rc = _account_query_step(hstmt);
4379 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));
4381 rc = _account_query_finalize(hstmt);
4382 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4388 if (hstmt != NULL) {
4389 rc = _account_query_finalize(hstmt);
4390 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4394 ret_transaction = _account_end_transaction(is_success);
4396 if (ret_transaction != ACCOUNT_ERROR_NONE) {
4397 ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
4399 if (is_success == true) {
4400 GSList* gs_iter = NULL;
4401 for (gs_iter = account_id_list; gs_iter != NULL; gs_iter = g_slist_next(gs_iter)) {
4402 char* p_tmpid = NULL;
4403 p_tmpid = (char*)gs_iter->data;
4405 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%s", ACCOUNT_NOTI_NAME_DELETE, p_tmpid);
4406 ACCOUNT_SLOGD("%s", buf);
4407 _account_insert_delete_update_notification_send(buf);
4408 _ACCOUNT_FREE(p_tmpid);
4410 g_slist_free(account_id_list);
4414 pthread_mutex_unlock(&account_mutex);
4416 _INFO("_account_delete_from_db_by_package_name end");
4420 int _account_get_total_count_from_db(gboolean include_hidden, int *count)
4423 ACCOUNT_SLOGE("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__);
4424 return ACCOUNT_ERROR_INVALID_PARAMETER;
4428 ACCOUNT_ERROR("DB is not opened\n");
4429 return ACCOUNT_ERROR_DB_NOT_OPENED;
4432 char query[1024] = {0, };
4433 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4437 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s", ACCOUNT_TABLE);
4441 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where secret = %d", ACCOUNT_TABLE, ACCOUNT_SECRECY_VISIBLE);
4444 *count = _account_get_record_count(query);
4446 if( _account_db_err_code() == SQLITE_PERM ){
4447 pthread_mutex_unlock(&account_mutex);
4448 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4449 return ACCOUNT_ERROR_PERMISSION_DENIED;
4454 account_stmt pStmt = NULL;
4456 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
4457 if (SQLITE_OK != rc) {
4458 ACCOUNT_SLOGE("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg());
4459 sqlite3_finalize(pStmt);
4460 return ACCOUNT_ERROR_DB_FAILED;
4463 rc = sqlite3_step(pStmt);
4464 if (SQLITE_ROW != rc) {
4465 ACCOUNT_ERROR("[ERROR] sqlite3_step() failed\n");
4466 sqlite3_finalize(pStmt);
4467 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4470 ncount = sqlite3_column_int(pStmt, 0);
4474 sqlite3_finalize(pStmt);
4477 ACCOUNT_ERROR("[ERROR] Number of account : %d, End", ncount);
4478 return ACCOUNT_ERROR_DB_FAILED;
4481 return ACCOUNT_ERROR_NONE;
4484 int account_type_create(account_type_h *account_type)
4486 if (!account_type) {
4487 ACCOUNT_SLOGE("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
4488 return ACCOUNT_ERROR_INVALID_PARAMETER;
4491 account_type_s *data = (account_type_s*)malloc(sizeof(account_type_s));
4494 ACCOUNT_ERROR("Memory Allocation Failed");
4495 return ACCOUNT_ERROR_OUT_OF_MEMORY;
4498 ACCOUNT_MEMSET(data, 0, sizeof(account_type_s));
4500 *account_type = (account_type_h)data;
4502 return ACCOUNT_ERROR_NONE;
4505 int account_type_destroy(account_type_h account_type)
4507 account_type_s *data = (account_type_s*)account_type;
4509 ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account type handle is null!"));
4511 _account_type_free_account_type_with_items(data);
4513 return ACCOUNT_ERROR_NONE;
4516 //app_id mandatory field
4517 int account_type_set_app_id(account_type_h account_type, const char *app_id)
4519 if (!account_type) {
4520 ACCOUNT_SLOGE("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
4521 return ACCOUNT_ERROR_INVALID_PARAMETER;
4525 ACCOUNT_SLOGE("(%s)-(%d) app_id is NULL.\n", __FUNCTION__, __LINE__);
4526 return ACCOUNT_ERROR_INVALID_PARAMETER;
4529 account_type_s *data = (account_type_s*)account_type;
4531 _ACCOUNT_FREE(data->app_id);
4532 data->app_id = _account_get_text(app_id);
4534 return ACCOUNT_ERROR_NONE;
4537 //service_provider_id mandatory field
4538 int account_type_set_service_provider_id(account_type_h account_type, const char *service_provider_id)
4540 if (!account_type) {
4541 return ACCOUNT_ERROR_INVALID_PARAMETER;
4544 if (!service_provider_id) {
4545 return ACCOUNT_ERROR_INVALID_PARAMETER;
4548 account_type_s *data = (account_type_s*)account_type;
4550 _ACCOUNT_FREE(data->service_provider_id);
4551 data->service_provider_id = _account_get_text(service_provider_id);
4553 return ACCOUNT_ERROR_NONE;
4556 int account_type_set_icon_path(account_type_h account_type, const char *icon_path)
4558 if (!account_type) {
4559 return ACCOUNT_ERROR_INVALID_PARAMETER;
4563 return ACCOUNT_ERROR_INVALID_PARAMETER;
4566 account_type_s *data = (account_type_s*)account_type;
4568 _ACCOUNT_FREE(data->icon_path);
4569 data->icon_path = _account_get_text(icon_path);
4571 return ACCOUNT_ERROR_NONE;
4574 int account_type_set_small_icon_path(account_type_h account_type, const char *small_icon_path)
4576 if (!account_type) {
4577 return ACCOUNT_ERROR_INVALID_PARAMETER;
4580 if (!small_icon_path) {
4581 return ACCOUNT_ERROR_INVALID_PARAMETER;
4584 account_type_s *data = (account_type_s*)account_type;
4586 _ACCOUNT_FREE(data->small_icon_path);
4587 data->small_icon_path = _account_get_text(small_icon_path);
4589 return ACCOUNT_ERROR_NONE;
4592 int account_type_set_multiple_account_support(account_type_h account_type, const bool multiple_account_support)
4594 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
4596 account_type_s *data = (account_type_s*)account_type;
4598 data->multiple_account_support = multiple_account_support;
4600 return ACCOUNT_ERROR_NONE;
4604 int account_type_set_label(account_type_h account_type, const char* label, const char* locale)
4606 if (!account_type) {
4607 ACCOUNT_SLOGE("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
4608 return ACCOUNT_ERROR_INVALID_PARAMETER;
4611 if(!label || !locale) {
4612 return ACCOUNT_ERROR_INVALID_PARAMETER;
4615 account_type_s *data = (account_type_s*)account_type;
4616 label_s *label_data = (label_s*)malloc(sizeof(label_s));
4618 if (label_data == NULL) {
4619 return ACCOUNT_ERROR_OUT_OF_MEMORY;
4621 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
4623 label_data->label = _account_get_text(label);
4624 label_data->locale = _account_get_text(locale);
4626 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
4628 return ACCOUNT_ERROR_NONE;
4631 int account_type_get_app_id(account_type_h account_type, char **app_id)
4633 if (!account_type) {
4634 return ACCOUNT_ERROR_INVALID_PARAMETER;
4638 return ACCOUNT_ERROR_INVALID_PARAMETER;
4641 account_type_s *data = (account_type_s*)account_type;
4644 *app_id = _account_get_text(data->app_id);
4646 return ACCOUNT_ERROR_NONE;
4649 int account_type_get_service_provider_id(account_type_h account_type, char **service_provider_id)
4651 if (!account_type) {
4652 return ACCOUNT_ERROR_INVALID_PARAMETER;
4655 if (!service_provider_id) {
4656 return ACCOUNT_ERROR_INVALID_PARAMETER;
4659 account_type_s *data = (account_type_s*)account_type;
4661 (*service_provider_id) = NULL;
4662 *service_provider_id = _account_get_text(data->service_provider_id);
4664 return ACCOUNT_ERROR_NONE;
4667 int account_type_get_icon_path(account_type_h account_type, char **icon_path)
4669 if (!account_type) {
4670 return ACCOUNT_ERROR_INVALID_PARAMETER;
4674 return ACCOUNT_ERROR_INVALID_PARAMETER;
4677 account_type_s *data = (account_type_s*)account_type;
4679 (*icon_path) = NULL;
4680 *icon_path = _account_get_text(data->icon_path);
4682 return ACCOUNT_ERROR_NONE;
4685 int account_type_get_small_icon_path(account_type_h account_type, char **small_icon_path)
4687 if (!account_type) {
4688 return ACCOUNT_ERROR_INVALID_PARAMETER;
4691 if (!small_icon_path) {
4692 return ACCOUNT_ERROR_INVALID_PARAMETER;
4695 account_type_s *data = (account_type_s*)account_type;
4697 (*small_icon_path) = NULL;
4698 *small_icon_path = _account_get_text(data->small_icon_path);
4700 return ACCOUNT_ERROR_NONE;
4703 int account_type_get_multiple_account_support(account_type_h account_type, int *multiple_account_support)
4705 if (!account_type) {
4706 return ACCOUNT_ERROR_INVALID_PARAMETER;
4708 if (!multiple_account_support) {
4709 return ACCOUNT_ERROR_INVALID_PARAMETER;
4712 account_type_s *data = (account_type_s*)account_type;
4714 *multiple_account_support = data->multiple_account_support;
4716 return ACCOUNT_ERROR_NONE;
4719 int account_type_get_label_by_locale(account_type_h account_type, const char* locale, char** label)
4721 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4722 ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID PARAMETER"));
4725 account_type_s *data = (account_type_s*)account_type;
4727 for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) {
4728 label_s *label_data = NULL;
4730 label_data = (label_s*)iter->data;
4734 if(!strcmp(locale, label_data->locale)) {
4735 *label = _account_get_text(label_data->label);
4736 return ACCOUNT_ERROR_NONE;
4740 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4743 int account_type_get_label(account_type_h account_type, account_label_cb callback, void *user_data)
4745 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4746 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4749 account_type_s *data = (account_type_s*)account_type;
4751 for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) {
4752 label_s *label_data = NULL;
4754 label_data = (label_s*)iter->data;
4756 if(callback(label_data->app_id, label_data->label, label_data->locale, user_data)!=TRUE) {
4757 ACCOUNT_DEBUG("Callback func returs FALSE, its iteration is stopped!!!!\n");
4758 return ACCOUNT_ERROR_NONE;
4762 return ACCOUNT_ERROR_NONE;
4765 static gboolean _account_type_check_duplicated(account_type_s *data)
4767 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4770 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4772 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId='%s'"
4773 , ACCOUNT_TYPE_TABLE, data->app_id);
4775 count = _account_get_record_count(query);
4783 static int _account_type_convert_account_to_sql(account_type_s *account_type, account_stmt hstmt, char *sql_value)
4789 /*Caution : Keep insert query orders.*/
4792 _account_query_bind_text(hstmt, count++, (char*)account_type->app_id);
4794 /* 2. service provider id*/
4795 _account_query_bind_text(hstmt, count++, (char*)account_type->service_provider_id);
4798 _account_query_bind_text(hstmt, count++, (char*)account_type->icon_path);
4800 /* 4. small icon path*/
4801 _account_query_bind_text(hstmt, count++, (char*)account_type->small_icon_path);
4803 /* 5. multiple accont support*/
4804 _account_query_bind_int(hstmt, count++, account_type->multiple_account_support);
4812 static int _account_type_execute_insert_query(account_type_s *account_type)
4817 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4818 int error_code = ACCOUNT_ERROR_NONE;
4819 account_stmt hstmt = NULL;
4821 /* check mandatory field */
4822 // app id & service provider id
4823 if (!account_type->app_id) {
4824 return ACCOUNT_ERROR_INVALID_PARAMETER;
4827 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4828 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s( AppId, ServiceProviderId , IconPath , SmallIconPath , MultipleAccountSupport ) values "
4829 "(?, ?, ?, ?, ?)", ACCOUNT_TYPE_TABLE);
4832 hstmt = _account_prepare_query(query);
4835 if( _account_db_err_code() == SQLITE_PERM ){
4836 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4837 return ACCOUNT_ERROR_PERMISSION_DENIED;
4838 } else if( _account_db_err_code() == SQLITE_BUSY ){
4839 ACCOUNT_ERROR( "Database Busy(%s)", _account_db_err_msg());
4840 return ACCOUNT_ERROR_DATABASE_BUSY;
4843 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4846 _account_type_convert_account_to_sql(account_type, hstmt, query);
4849 rc = _account_query_step(hstmt);
4850 if (rc == SQLITE_BUSY) {
4851 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4852 error_code = ACCOUNT_ERROR_DATABASE_BUSY;
4853 } else if (rc != SQLITE_DONE) {
4854 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4855 error_code = ACCOUNT_ERROR_DB_FAILED;
4859 rc = _account_query_finalize(hstmt);
4860 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4867 static int _account_type_insert_label(account_type_s *account_type)
4870 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4871 account_stmt hstmt = NULL;
4873 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4875 if (g_slist_length( account_type->label_list)==0) {
4876 ACCOUNT_ERROR( "_account_type_insert_label, no label\n");
4877 return ACCOUNT_ERROR_NONE;
4880 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId = '%s'", ACCOUNT_TYPE_TABLE, account_type->app_id);
4882 rc = _account_get_record_count(query);
4884 if( _account_db_err_code() == SQLITE_PERM ){
4885 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4886 return ACCOUNT_ERROR_PERMISSION_DENIED;
4890 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4896 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
4899 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4900 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
4901 "(?, ?, ?) ", LABEL_TABLE);
4903 hstmt = _account_prepare_query(query);
4905 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4907 label_s* label_data = NULL;
4908 label_data = (label_s*)iter->data;
4910 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
4911 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4912 ret = _account_query_bind_text(hstmt, count++, label_data->label);
4913 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4914 ret = _account_query_bind_text(hstmt, count++, (char*)label_data->locale);
4915 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4917 rc = _account_query_step(hstmt);
4919 if (rc != SQLITE_DONE) {
4920 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4924 rc = _account_query_finalize(hstmt);
4925 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
4930 return ACCOUNT_ERROR_NONE;
4933 static void _account_type_convert_column_to_provider_feature(account_stmt hstmt, provider_feature_s *feature_record)
4935 const char *textbuf = NULL;
4937 textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_APP_ID);
4938 _account_db_data_to_text(textbuf, &(feature_record->app_id));
4940 textbuf = _account_query_table_column_text(hstmt, PROVIDER_FEATURE_FIELD_KEY);
4941 _account_db_data_to_text(textbuf, &(feature_record->key));
4945 GSList* _account_type_query_provider_feature_by_app_id_from_global_db(const char* app_id, int *error_code)
4947 _INFO("_account_type_query_provider_feature_by_app_id_in_global_db app_id=%s", app_id);
4948 account_stmt hstmt = NULL;
4949 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4950 int rc = 0, binding_count = 1;
4951 GSList* feature_list = NULL;
4953 ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
4954 ACCOUNT_RETURN_VAL((error_code != NULL), {_ERR("error_code pointer is NULL");}, NULL, (""));
4955 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER; _ERR("The database isn't connected.");}, NULL, ("The database isn't connected."));
4957 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4959 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
4960 _INFO("account query=[%s]", query);
4962 hstmt = _account_prepare_query_from_global_db(query);
4964 if( _account_db_err_code_from_global_db() == SQLITE_PERM ){
4965 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
4966 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
4970 _INFO("before _account_query_bind_text");
4971 _account_query_bind_text(hstmt, binding_count++, app_id);
4973 rc = _account_query_step(hstmt);
4975 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {*error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND; _ERR("The record isn't found from global db. rc=[%d]", rc);}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4977 provider_feature_s* feature_record = NULL;
4979 while (rc == SQLITE_ROW) {
4981 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
4983 if (feature_record == NULL) {
4984 ACCOUNT_FATAL("malloc Failed");
4988 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
4990 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
4992 _INFO("Adding account feature_list");
4993 feature_list = g_slist_append(feature_list, feature_record);
4995 rc = _account_query_step(hstmt);
4998 *error_code = ACCOUNT_ERROR_NONE;
5001 if (hstmt != NULL) {
5002 rc = _account_query_finalize_from_global_db(hstmt);
5003 if (rc != ACCOUNT_ERROR_NONE) {
5005 _ERR("global db fianlize error");
5009 if (*error_code != ACCOUNT_ERROR_NONE) {
5010 _account_type_gslist_feature_free(feature_list);
5013 _INFO("Returning account feature_list from global db");
5014 return feature_list;
5017 GSList* _account_type_query_provider_feature_by_app_id(const char* app_id, int *error_code)
5019 _INFO("_account_type_query_provider_feature_by_app_id app_id=%s", app_id);
5020 account_stmt hstmt = NULL;
5021 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5022 int rc = 0, binding_count = 1;
5023 GSList* feature_list = NULL;
5025 ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
5026 ACCOUNT_RETURN_VAL((error_code != NULL), {_ERR("error_code pointer is NULL");}, NULL, (""));
5027 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
5029 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5031 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
5032 _INFO("account query=[%s]", query);
5034 hstmt = _account_prepare_query(query);
5036 if( _account_db_err_code() == SQLITE_PERM ){
5037 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5038 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
5042 _account_query_bind_text(hstmt, binding_count++, app_id);
5044 rc = _account_query_step(hstmt);
5046 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {*error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND; _ERR("The record isn't found from user db. rc=[%d]", rc);}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5048 provider_feature_s* feature_record = NULL;
5050 while (rc == SQLITE_ROW) {
5052 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
5054 if (feature_record == NULL) {
5055 ACCOUNT_FATAL("malloc Failed");
5059 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
5061 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
5063 _INFO("Adding account feature_list");
5064 feature_list = g_slist_append(feature_list, feature_record);
5066 rc = _account_query_step(hstmt);
5069 *error_code = ACCOUNT_ERROR_NONE;
5071 rc = _account_query_finalize(hstmt);
5072 ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, rc, ("account finalize error"));
5076 if (hstmt != NULL) {
5077 rc = _account_query_finalize(hstmt);
5078 if (rc != ACCOUNT_ERROR_NONE) {
5080 _ERR("account fianlize error");
5084 _INFO("*error_code=[%d]", *error_code);
5086 if (*error_code == ACCOUNT_ERROR_RECORD_NOT_FOUND) {
5087 feature_list = _account_type_query_provider_feature_by_app_id_from_global_db(app_id, error_code);
5090 if (*error_code != ACCOUNT_ERROR_NONE)
5091 _account_type_gslist_feature_free(feature_list);
5093 _INFO("Returning account feature_list");
5094 return feature_list;
5097 int _account_type_query_provider_feature_cb_by_app_id_from_global_db(provider_feature_cb callback, const char* app_id, void *user_data )
5099 int error_code = ACCOUNT_ERROR_NONE;
5100 account_stmt hstmt = NULL;
5101 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5102 int rc = 0, binding_count = 1;
5104 _INFO("_account_type_query_provider_feature_cb_by_app_id_in_global_db start app_id=%s", app_id);
5105 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5106 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
5107 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5109 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5111 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
5112 hstmt = _account_prepare_query_from_global_db(query);
5114 if( _account_db_err_code_from_global_db() == SQLITE_PERM ){
5115 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg_from_global_db());
5116 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_PERMISSION_DENIED, ("global db permission denied.\n"));
5119 _account_query_bind_text(hstmt, binding_count++, app_id);
5121 rc = _account_query_step(hstmt);
5122 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {_ERR("The record isn't found. rc=[%d]", rc);}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5124 provider_feature_s* feature_record = NULL;
5126 while (rc == SQLITE_ROW) {
5127 bool cb_ret = FALSE;
5128 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
5130 if (feature_record == NULL) {
5131 ACCOUNT_FATAL("malloc Failed");
5135 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
5137 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
5139 cb_ret = callback(feature_record->app_id, feature_record->key, user_data);
5141 _account_type_free_feature_with_items(feature_record);
5143 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returns FALSE, its iteration is stopped!!!!\n"));
5145 rc = _account_query_step(hstmt);
5148 error_code = ACCOUNT_ERROR_NONE;
5151 if (hstmt != NULL) {
5152 rc = _account_query_finalize_from_global_db(hstmt);
5153 if (rc != ACCOUNT_ERROR_NONE) {
5155 _ERR("global db finalize error[%d]", rc);
5160 _INFO("_account_type_query_provider_feature_cb_by_app_id_in_global_db end. error_code=[%d]", error_code);
5164 int _account_type_query_provider_feature_cb_by_app_id(provider_feature_cb callback, const char* app_id, void *user_data )
5166 int error_code = ACCOUNT_ERROR_NONE;
5167 account_stmt hstmt = NULL;
5168 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5169 int rc = 0, binding_count = 1;
5171 _INFO("_account_type_query_provider_feature_cb_by_app_id start app_id=%s", app_id);
5172 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5173 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
5174 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5176 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5178 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE app_id = ?", PROVIDER_FEATURE_TABLE);
5179 hstmt = _account_prepare_query(query);
5181 if( _account_db_err_code() == SQLITE_PERM ){
5182 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5183 return ACCOUNT_ERROR_PERMISSION_DENIED;
5186 _account_query_bind_text(hstmt, binding_count++, app_id);
5188 rc = _account_query_step(hstmt);
5189 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found in user db.\n"));
5191 provider_feature_s* feature_record = NULL;
5193 while (rc == SQLITE_ROW) {
5194 bool cb_ret = FALSE;
5195 feature_record = (provider_feature_s*) malloc(sizeof(provider_feature_s));
5197 if (feature_record == NULL) {
5198 ACCOUNT_FATAL("malloc Failed");
5202 ACCOUNT_MEMSET(feature_record, 0x00, sizeof(provider_feature_s));
5204 _account_type_convert_column_to_provider_feature(hstmt, feature_record);
5206 cb_ret = callback(feature_record->app_id, feature_record->key, user_data);
5208 _account_type_free_feature_with_items(feature_record);
5210 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
5212 rc = _account_query_step(hstmt);
5215 rc = _account_query_finalize(hstmt);
5216 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5219 error_code = ACCOUNT_ERROR_NONE;
5222 if (hstmt != NULL) {
5223 rc = _account_query_finalize(hstmt);
5224 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5228 if (error_code == ACCOUNT_ERROR_RECORD_NOT_FOUND) {
5229 error_code = _account_type_query_provider_feature_cb_by_app_id_from_global_db(callback, app_id, user_data);
5232 _INFO("_account_type_query_provider_feature_cb_by_app_id end");
5236 int account_type_query_provider_feature_cb_by_app_id(provider_feature_cb callback, const char* app_id, void *user_data )
5238 int error_code = ACCOUNT_ERROR_NONE;
5240 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5241 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
5242 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5244 error_code = _account_type_query_provider_feature_cb_by_app_id(callback, app_id, user_data);
5246 if (error_code == ACCOUNT_ERROR_RECORD_NOT_FOUND) {
5247 error_code = _account_type_query_provider_feature_cb_by_app_id_from_global_db(callback, app_id, user_data);
5253 bool _account_type_query_supported_feature_from_global_db(const char* app_id, const char* capability, int *error_code)
5255 _INFO("_account_type_query_supported_feature_in_global_db start");
5256 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5258 *error_code = ACCOUNT_ERROR_NONE;
5260 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5261 int record_count = 0;
5263 if (app_id == NULL || capability == NULL)
5265 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
5269 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
5271 record_count = _account_get_record_count_from_global_db(query);
5273 if( _account_db_err_code_from_global_db() == SQLITE_PERM ){
5274 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg_from_global_db());
5275 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
5278 if (record_count <= 0)
5280 *error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
5284 _INFO("_account_type_query_supported_feature_in_global_db end");
5288 bool _account_type_query_supported_feature(const char* app_id, const char* capability, int *error_code)
5290 _INFO("_account_type_query_supported_feature start");
5292 *error_code = ACCOUNT_ERROR_NONE;
5294 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5295 int record_count = 0;
5297 if (app_id == NULL || capability == NULL)
5299 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
5303 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s where app_id='%s' and key='%s'", PROVIDER_FEATURE_TABLE, app_id, capability);
5305 record_count = _account_get_record_count(query);
5307 if( _account_db_err_code() == SQLITE_PERM ){
5308 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5309 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
5313 if (record_count <= 0)
5315 bool is_exist = false;
5316 is_exist = _account_type_query_supported_feature_from_global_db(app_id, capability, error_code);
5321 _INFO("_account_type_query_supported_feature end");
5325 static int _account_type_insert_provider_feature(account_type_s *account_type, const char* app_id)
5328 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5329 account_stmt hstmt = NULL;
5331 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
5332 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5334 if (g_slist_length( account_type->provider_feature_list)==0) {
5335 ACCOUNT_ERROR( "no capability\n");
5336 return ACCOUNT_ERROR_NONE;
5339 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId='%s'", ACCOUNT_TYPE_TABLE, app_id);
5341 rc = _account_get_record_count(query);
5343 if( _account_db_err_code() == SQLITE_PERM ){
5344 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5345 return ACCOUNT_ERROR_PERMISSION_DENIED;
5349 ACCOUNT_SLOGI( "related account type item is not existed rc=%d , %s", rc, _account_db_err_msg());
5350 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5357 for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
5360 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5361 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
5362 "(?, ?) ", PROVIDER_FEATURE_TABLE);
5364 hstmt = _account_prepare_query(query);
5366 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5368 provider_feature_s* feature_data = NULL;
5369 feature_data = (provider_feature_s*)iter->data;
5371 ret = _account_query_bind_text(hstmt, count++, app_id);
5372 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5373 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
5374 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
5376 rc = _account_query_step(hstmt);
5378 if (rc != SQLITE_DONE) {
5379 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5383 rc = _account_query_finalize(hstmt);
5384 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5389 return ACCOUNT_ERROR_NONE;
5392 int _account_type_insert_to_db(account_type_s* account_type, int* account_type_id)
5396 int error_code = ACCOUNT_ERROR_NONE, ret_transaction = 0;
5398 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5399 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL"));
5400 ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID POINTER IS NULL"));
5402 account_type_s *data = (account_type_s*)account_type;
5404 pthread_mutex_lock(&account_mutex);
5407 /* transaction control required*/
5408 ret_transaction = _account_begin_transaction();
5412 if( _account_db_err_code() == SQLITE_PERM ){
5413 pthread_mutex_unlock(&account_mutex);
5414 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5415 return ACCOUNT_ERROR_PERMISSION_DENIED;
5419 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
5420 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
5421 pthread_mutex_unlock(&account_mutex);
5422 return ACCOUNT_ERROR_DATABASE_BUSY;
5423 } else if (ret_transaction != ACCOUNT_ERROR_NONE) {
5424 ACCOUNT_ERROR("_account_begin_transaction fail %d\n", ret_transaction);
5425 pthread_mutex_unlock(&account_mutex);
5426 return ret_transaction;
5430 if (_account_type_check_duplicated(data)) {
5432 ret_transaction = _account_end_transaction(FALSE);
5433 ACCOUNT_ERROR("Duplicated, rollback insert query(%x)!!!!\n", ret_transaction);
5434 *account_type_id = -1;
5435 pthread_mutex_unlock(&account_mutex);
5436 return ACCOUNT_ERROR_DUPLICATED;
5439 *account_type_id = _account_get_next_sequence(ACCOUNT_TYPE_TABLE);
5441 error_code = _account_type_execute_insert_query(data);
5443 if (error_code != ACCOUNT_ERROR_NONE){
5444 error_code = ACCOUNT_ERROR_DUPLICATED;
5445 ret_transaction = _account_end_transaction(FALSE);
5446 ACCOUNT_ERROR("Insert fail, rollback insert query(%x)!!!!\n", ret_transaction);
5447 *account_type_id = -1;
5448 pthread_mutex_unlock(&account_mutex);
5454 error_code = _account_type_insert_provider_feature(data, data->app_id);
5455 if(error_code != ACCOUNT_ERROR_NONE) {
5457 ret_transaction = _account_end_transaction(FALSE);
5458 ACCOUNT_ERROR("Insert provider feature fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
5459 pthread_mutex_unlock(&account_mutex);
5463 error_code = _account_type_insert_label(data);
5464 if(error_code != ACCOUNT_ERROR_NONE) {
5466 ret_transaction = _account_end_transaction(FALSE);
5467 ACCOUNT_ERROR("Insert label fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
5468 pthread_mutex_unlock(&account_mutex);
5472 ret_transaction = _account_end_transaction(TRUE);
5474 pthread_mutex_unlock(&account_mutex);
5477 return ACCOUNT_ERROR_NONE;
5480 static int _account_type_update_provider_feature(account_type_s *account_type, const char* app_id)
5483 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5484 account_stmt hstmt = NULL;
5486 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
5488 if (g_slist_length( account_type->provider_feature_list)==0) {
5489 ACCOUNT_ERROR( "no feature\n");
5490 return ACCOUNT_ERROR_NONE;
5493 ACCOUNT_DEBUG( "app id", app_id);
5495 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5497 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id=? ", PROVIDER_FEATURE_TABLE);
5498 hstmt = _account_prepare_query(query);
5500 if( _account_db_err_code() == SQLITE_PERM ){
5501 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5502 return ACCOUNT_ERROR_PERMISSION_DENIED;
5506 _account_query_bind_text(hstmt, count++, app_id);
5507 rc = _account_query_step(hstmt);
5509 if (rc != SQLITE_DONE) {
5510 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5511 return ACCOUNT_ERROR_DB_FAILED;
5513 rc = _account_query_finalize(hstmt);
5514 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5519 for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
5522 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5523 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
5524 "(?, ?) ", PROVIDER_FEATURE_TABLE);
5526 hstmt = _account_prepare_query(query);
5528 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5530 provider_feature_s* feature_data = NULL;
5531 feature_data = (provider_feature_s*)iter->data;
5533 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
5534 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5535 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
5536 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5538 rc = _account_query_step(hstmt);
5540 if (rc != SQLITE_DONE) {
5541 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5544 rc = _account_query_finalize(hstmt);
5545 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5549 return ACCOUNT_ERROR_NONE;
5552 static int _account_type_update_label(account_type_s *account_type, const char* app_id)
5555 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5556 account_stmt hstmt = NULL;
5558 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
5560 if (g_slist_length( account_type->label_list)==0) {
5561 return ACCOUNT_ERROR_NONE;
5564 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5566 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId=? ", LABEL_TABLE);
5567 hstmt = _account_prepare_query(query);
5569 if( _account_db_err_code() == SQLITE_PERM ){
5570 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5571 return ACCOUNT_ERROR_PERMISSION_DENIED;
5575 _account_query_bind_text(hstmt, count++, app_id);
5576 rc = _account_query_step(hstmt);
5578 if (rc != SQLITE_DONE) {
5579 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5580 return ACCOUNT_ERROR_DB_FAILED;
5582 rc = _account_query_finalize(hstmt);
5583 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5588 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
5591 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5592 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
5593 "(?, ?, ?) ", LABEL_TABLE);
5595 hstmt = _account_prepare_query(query);
5597 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
5599 label_s* label_data = NULL;
5600 label_data = (label_s*)iter->data;
5602 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
5603 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5604 ret = _account_query_bind_text(hstmt, count++, label_data->label);
5605 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5606 ret = _account_query_bind_text(hstmt, count++, label_data->locale);
5607 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
5609 rc = _account_query_step(hstmt);
5611 if (rc != SQLITE_DONE) {
5612 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5615 rc = _account_query_finalize(hstmt);
5616 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5620 return ACCOUNT_ERROR_NONE;
5624 static int _account_type_update_account(account_type_s *account_type, const char* app_id)
5626 int rc = 0, binding_count =1;
5627 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5628 int error_code = ACCOUNT_ERROR_NONE;
5629 account_stmt hstmt = NULL;
5631 if (!account_type->app_id) {
5632 ACCOUNT_ERROR("app id is mandetory field, it can not be NULL!!!!\n");
5633 return ACCOUNT_ERROR_INVALID_PARAMETER;
5636 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
5637 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET AppId=?, ServiceProviderId=?, IconPath=?, "
5638 "SmallIconPath=?, MultipleAccountSupport=? WHERE AppId=? ", ACCOUNT_TYPE_TABLE);
5640 hstmt = _account_prepare_query(query);
5642 if( _account_db_err_code() == SQLITE_PERM ){
5643 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5644 return ACCOUNT_ERROR_PERMISSION_DENIED;
5645 } else if (_account_db_err_code() == SQLITE_BUSY){
5646 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
5647 return ACCOUNT_ERROR_DATABASE_BUSY;
5650 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
5652 binding_count = _account_type_convert_account_to_sql(account_type, hstmt, query);
5653 _account_query_bind_text(hstmt, binding_count++, app_id);
5655 rc = _account_query_step(hstmt);
5656 if (rc != SQLITE_DONE) {
5657 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
5660 rc = _account_query_finalize(hstmt);
5661 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5665 error_code = _account_type_update_label(account_type, app_id);
5666 /* update provider feature */
5667 error_code = _account_type_update_provider_feature(account_type, app_id);
5672 int _account_type_update_to_db_by_app_id(account_type_s* account_type, const char* app_id)
5674 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
5675 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5676 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5678 int error_code = ACCOUNT_ERROR_NONE;
5679 account_type_s* data = account_type;
5681 pthread_mutex_lock(&account_mutex);
5683 error_code = _account_type_update_account(data, app_id);
5685 pthread_mutex_unlock(&account_mutex);
5690 int _account_type_delete_by_app_id(const char* app_id)
5692 int error_code = ACCOUNT_ERROR_NONE;
5693 account_stmt hstmt = NULL;
5694 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5695 int rc = 0, count = -1;
5696 int ret_transaction = 0;
5697 int binding_count = 1;
5698 bool is_success = FALSE;
5700 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5701 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("The database isn't connected."));
5703 /* Check requested ID to delete */
5704 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
5706 count = _account_get_record_count(query);
5708 if( _account_db_err_code() == SQLITE_PERM ){
5709 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5710 return ACCOUNT_ERROR_PERMISSION_DENIED;
5714 ACCOUNT_SLOGE("app id(%s) is not exist. count(%d)\n", app_id, count);
5715 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
5718 /* transaction control required*/
5719 ret_transaction = _account_begin_transaction();
5721 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
5722 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
5723 pthread_mutex_unlock(&account_mutex);
5724 return ACCOUNT_ERROR_DATABASE_BUSY;
5725 }else if (ret_transaction != ACCOUNT_ERROR_NONE) {
5726 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
5727 pthread_mutex_unlock(&account_mutex);
5728 return ret_transaction;
5731 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", LABEL_TABLE);
5733 hstmt = _account_prepare_query(query);
5735 if( _account_db_err_code() == SQLITE_PERM ){
5736 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5737 pthread_mutex_unlock(&account_mutex);
5738 return ACCOUNT_ERROR_PERMISSION_DENIED;
5741 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
5742 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
5744 _account_query_bind_text(hstmt, binding_count++, app_id);
5746 rc = _account_query_step(hstmt);
5747 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5749 rc = _account_query_finalize(hstmt);
5750 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5754 ACCOUNT_MEMSET(query, 0, sizeof(query));
5756 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id = ? ", PROVIDER_FEATURE_TABLE);
5758 hstmt = _account_prepare_query(query);
5759 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
5760 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
5762 _account_query_bind_text(hstmt, binding_count++, app_id);
5764 rc = _account_query_step(hstmt);
5765 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
5767 rc = _account_query_finalize(hstmt);
5768 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5774 ACCOUNT_MEMSET(query, 0, sizeof(query));
5776 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ? ", ACCOUNT_TYPE_TABLE);
5778 hstmt = _account_prepare_query(query);
5779 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
5780 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
5782 _account_query_bind_text(hstmt, binding_count++, app_id);
5784 rc = _account_query_step(hstmt);
5785 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
5787 rc = _account_query_finalize(hstmt);
5788 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5794 if (hstmt != NULL) {
5795 rc = _account_query_finalize(hstmt);
5796 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
5800 ret_transaction = _account_end_transaction(is_success);
5802 if (ret_transaction != ACCOUNT_ERROR_NONE) {
5803 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
5806 pthread_mutex_unlock(&account_mutex);
5811 static void _account_type_convert_column_to_account_type(account_stmt hstmt, account_type_s *account_type_record)
5813 const char *textbuf = NULL;
5815 account_type_record->id = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_ID);
5817 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_APP_ID);
5818 _account_db_data_to_text(textbuf, &(account_type_record->app_id));
5820 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SERVICE_PROVIDER_ID);
5821 _account_db_data_to_text(textbuf, &(account_type_record->service_provider_id));
5823 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_ICON_PATH);
5824 _account_db_data_to_text(textbuf, &(account_type_record->icon_path));
5826 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SMALL_ICON_PATH);
5827 _account_db_data_to_text(textbuf, &(account_type_record->small_icon_path));
5829 account_type_record->multiple_account_support = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_MULTIPLE_ACCOUNT_SUPPORT);
5833 static void _account_type_convert_column_to_label(account_stmt hstmt, label_s *label_record)
5835 const char *textbuf = NULL;
5837 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_APP_ID);
5838 _account_db_data_to_text(textbuf, &(label_record->app_id));
5840 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LABEL);
5841 _account_db_data_to_text(textbuf, &(label_record->label));
5843 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LOCALE);
5844 _account_db_data_to_text(textbuf, &(label_record->locale));
5848 GSList* _account_type_get_label_list_by_app_id_from_global_db(const char* app_id, int *error_code )
5850 *error_code = ACCOUNT_ERROR_NONE;
5851 account_stmt hstmt = NULL;
5852 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5853 int rc = 0, binding_count = 1;
5854 GSList* label_list = NULL;
5856 ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
5857 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5859 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5861 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
5862 hstmt = _account_prepare_query_from_global_db(query);
5864 if( _account_db_err_code_from_global_db() == SQLITE_PERM ){
5865 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg_from_global_db());
5866 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
5871 _account_query_bind_text(hstmt, binding_count++, app_id);
5873 rc = _account_query_step(hstmt);
5874 ACCOUNT_CATCH_ERROR_P((rc == SQLITE_ROW), {_ERR("The record isn't found. rc=[%d] done", rc);}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5876 label_s* label_record = NULL;
5878 while (rc == SQLITE_ROW) {
5879 label_record = (label_s*) malloc(sizeof(label_s));
5881 if (label_record == NULL) {
5882 ACCOUNT_FATAL("malloc Failed");
5886 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
5888 _account_type_convert_column_to_label(hstmt, label_record);
5890 _INFO("Adding account label_list");
5891 label_list = g_slist_append (label_list, label_record);
5893 rc = _account_query_step(hstmt);
5896 *error_code = ACCOUNT_ERROR_NONE;
5899 if (hstmt != NULL) {
5900 rc = _account_query_finalize_from_global_db(hstmt);
5901 if (rc != ACCOUNT_ERROR_NONE) {
5902 _ERR("global db finalize error[%d]", rc);
5907 _INFO("Returning account global label_list");
5911 GSList* _account_type_get_label_list_by_app_id(const char* app_id, int *error_code )
5913 *error_code = ACCOUNT_ERROR_NONE;
5914 account_stmt hstmt = NULL;
5915 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5916 int rc = 0, binding_count = 1;
5917 GSList* label_list = NULL;
5919 ACCOUNT_RETURN_VAL((app_id != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("APP ID IS NULL"));
5920 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
5922 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5924 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
5925 hstmt = _account_prepare_query(query);
5927 if( _account_db_err_code() == SQLITE_PERM ){
5928 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
5929 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
5933 _account_query_bind_text(hstmt, binding_count++, app_id);
5935 rc = _account_query_step(hstmt);
5936 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
5938 label_s* label_record = NULL;
5940 while (rc == SQLITE_ROW) {
5941 label_record = (label_s*) malloc(sizeof(label_s));
5943 if (label_record == NULL) {
5944 ACCOUNT_FATAL("malloc Failed");
5948 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
5950 _account_type_convert_column_to_label(hstmt, label_record);
5952 _INFO("Adding account label_list");
5953 label_list = g_slist_append (label_list, label_record);
5955 rc = _account_query_step(hstmt);
5958 rc = _account_query_finalize(hstmt);
5959 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
5962 *error_code = ACCOUNT_ERROR_NONE;
5965 if (hstmt != NULL) {
5966 rc = _account_query_finalize(hstmt);
5967 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
5971 if (*error_code == ACCOUNT_ERROR_RECORD_NOT_FOUND) {
5972 label_list = _account_type_get_label_list_by_app_id_from_global_db(app_id, error_code);
5975 _INFO("Returning account label_list");
5979 int _account_type_query_label_by_app_id_from_global_db(account_label_cb callback, const char* app_id, void *user_data )
5981 int error_code = ACCOUNT_ERROR_NONE;
5982 account_stmt hstmt = NULL;
5983 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
5984 int rc = 0, binding_count = 1;
5986 _INFO("account_type_query_label_by_app_id_from_global_db start");
5988 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
5989 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
5990 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
5992 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
5994 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
5995 hstmt = _account_prepare_query_from_global_db(query);
5997 if( _account_db_err_code_from_global_db() == SQLITE_PERM ){
5998 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg_from_global_db());
5999 error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
6003 _account_query_bind_text(hstmt, binding_count++, app_id);
6005 rc = _account_query_step(hstmt);
6006 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
6008 label_s* label_record = NULL;
6010 while (rc == SQLITE_ROW) {
6011 bool cb_ret = FALSE;
6012 label_record = (label_s*) malloc(sizeof(label_s));
6014 if (label_record == NULL) {
6015 ACCOUNT_FATAL("malloc Failed");
6019 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
6021 _account_type_convert_column_to_label(hstmt, label_record);
6023 cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data);
6025 _account_type_free_label_with_items(label_record);
6027 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
6029 rc = _account_query_step(hstmt);
6032 error_code = ACCOUNT_ERROR_NONE;
6035 if (hstmt != NULL) {
6036 rc = _account_query_finalize_from_global_db(hstmt);
6037 if (rc != ACCOUNT_ERROR_NONE) {
6038 _ERR("global db finalize error[%d]", rc);
6043 _INFO("account_type_query_label_by_app_id_from_global_db end [%d]", error_code);
6047 int _account_type_query_label_by_app_id(account_label_cb callback, const char* app_id, void *user_data )
6049 int error_code = ACCOUNT_ERROR_NONE;
6050 account_stmt hstmt = NULL;
6051 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
6052 int rc = 0, binding_count = 1;
6054 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
6055 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
6056 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
6058 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
6060 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
6061 hstmt = _account_prepare_query(query);
6063 if( _account_db_err_code() == SQLITE_PERM ){
6064 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
6065 return ACCOUNT_ERROR_PERMISSION_DENIED;
6068 _account_query_bind_text(hstmt, binding_count++, app_id);
6070 rc = _account_query_step(hstmt);
6071 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
6073 label_s* label_record = NULL;
6075 while (rc == SQLITE_ROW) {
6076 bool cb_ret = FALSE;
6077 label_record = (label_s*) malloc(sizeof(label_s));
6079 if (label_record == NULL) {
6080 ACCOUNT_FATAL("malloc Failed");
6084 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
6086 _account_type_convert_column_to_label(hstmt, label_record);
6088 cb_ret = callback(label_record->app_id, label_record->label , label_record->locale, user_data);
6090 _account_type_free_label_with_items(label_record);
6092 // ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
6093 if(cb_ret == TRUE) {
6094 _INFO("Callback func returs FALSE, its iteration is stopped!!!!\n");
6098 rc = _account_query_step(hstmt);
6101 rc = _account_query_finalize(hstmt);
6102 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6105 error_code = ACCOUNT_ERROR_NONE;
6108 if (hstmt != NULL) {
6109 rc = _account_query_finalize(hstmt);
6110 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6114 if (error_code == ACCOUNT_ERROR_RECORD_NOT_FOUND) {
6115 error_code = account_type_query_label_by_app_id_from_global_db(callback, app_id, user_data);
6121 int account_type_query_label_by_app_id(account_label_cb callback, const char* app_id, void *user_data )
6123 int error_code = ACCOUNT_ERROR_NONE;
6125 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
6126 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
6127 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
6129 error_code = _account_type_query_label_by_app_id(callback, app_id, user_data);
6131 if (error_code == ACCOUNT_ERROR_RECORD_NOT_FOUND) {
6132 error_code = _account_type_query_label_by_app_id_from_global_db(callback, app_id, user_data);
6138 int _account_type_label_get_app_id(label_h label, char **app_id)
6141 return ACCOUNT_ERROR_INVALID_PARAMETER;
6145 return ACCOUNT_ERROR_INVALID_PARAMETER;
6148 label_s *data = (label_s*)label;
6152 *app_id = _account_get_text(data->app_id);
6154 return ACCOUNT_ERROR_NONE;
6157 bool _account_get_label_text_cb(char* app_id, char* label, char* locale, void *user_data)
6159 account_type_s *data = (account_type_s*)user_data;
6161 label_s *label_data = (label_s*)malloc(sizeof(label_s));
6163 if (label_data == NULL) {
6164 ACCOUNT_FATAL("_account_get_label_text_cb : MALLOC FAIL\n");
6167 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
6169 label_data->app_id = _account_get_text(app_id);
6170 label_data->label = _account_get_text(label);
6171 label_data->locale = _account_get_text(locale);
6173 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
6178 bool _account_get_provider_feature_cb(char* app_id, char* key, void* user_data)
6180 account_type_s *data = (account_type_s*)user_data;
6182 provider_feature_s *feature_data = (provider_feature_s*)malloc(sizeof(provider_feature_s));
6184 if (feature_data == NULL) {
6185 ACCOUNT_FATAL("_account_get_provider_feature_cb : MALLOC FAIL\n");
6188 ACCOUNT_MEMSET(feature_data, 0, sizeof(provider_feature_s));
6190 feature_data->app_id = _account_get_text(app_id);
6191 feature_data->key = _account_get_text(key);
6193 data->provider_feature_list = g_slist_append(data->provider_feature_list, (gpointer)feature_data);
6198 int _account_type_query_by_app_id_from_global_db(const char* app_id, account_type_s** account_type_record)
6200 _INFO("_account_type_query_by_app_id_from_global_db start");
6202 int error_code = ACCOUNT_ERROR_NONE;
6203 account_stmt hstmt = NULL;
6204 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
6205 int rc = 0, binding_count = 1;
6207 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
6208 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
6209 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
6211 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
6213 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
6214 hstmt = _account_prepare_query_from_global_db(query);
6216 if( _account_db_err_code_from_global_db() == SQLITE_PERM ){
6217 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg_from_global_db());
6218 return ACCOUNT_ERROR_PERMISSION_DENIED;
6221 _account_query_bind_text(hstmt, binding_count++, app_id);
6223 rc = _account_query_step(hstmt);
6224 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
6226 *account_type_record = create_empty_account_type_instance();
6228 while (rc == SQLITE_ROW) {
6229 _account_type_convert_column_to_account_type(hstmt, *account_type_record);
6230 rc = _account_query_step(hstmt);
6233 rc = _account_query_finalize_from_global_db(hstmt);
6234 ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {_ERR("global db finalize error rc=[%d]", rc);}, rc, ("finalize error"));
6235 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb, app_id, (void*)(*account_type_record));
6236 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, app_id,(void*)(*account_type_record));
6239 error_code = ACCOUNT_ERROR_NONE;
6242 if (hstmt != NULL) {
6243 rc = _account_query_finalize(hstmt);
6244 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6248 _INFO("_account_type_query_by_app_id_from_global_db end [%d]", error_code);
6252 int _account_type_query_by_app_id(const char* app_id, account_type_s** account_type_record)
6254 _INFO("_account_type_query_by_app_id start");
6256 int error_code = ACCOUNT_ERROR_NONE;
6257 account_stmt hstmt = NULL;
6258 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
6259 int rc = 0, binding_count = 1;
6261 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
6262 ACCOUNT_RETURN_VAL((account_type_record != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account type record(account_type_s**) is NULL"));
6263 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
6265 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
6267 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
6268 hstmt = _account_prepare_query(query);
6270 if( _account_db_err_code() == SQLITE_PERM ){
6271 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
6272 return ACCOUNT_ERROR_PERMISSION_DENIED;
6275 _account_query_bind_text(hstmt, binding_count++, app_id);
6277 rc = _account_query_step(hstmt);
6278 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
6280 *account_type_record = create_empty_account_type_instance();
6281 if (*account_type_record == NULL) {
6282 _ERR("Out of Memory");
6283 error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
6287 while (rc == SQLITE_ROW) {
6288 _account_type_convert_column_to_account_type(hstmt, *account_type_record);
6289 rc = _account_query_step(hstmt);
6292 rc = _account_query_finalize(hstmt);
6293 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6294 _account_type_query_label_by_app_id(_account_get_label_text_cb, app_id, (void*)(*account_type_record));
6295 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, app_id,(void*)(*account_type_record));
6298 error_code = ACCOUNT_ERROR_NONE;
6301 if (hstmt != NULL) {
6302 rc = _account_query_finalize(hstmt);
6303 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6307 if (error_code == ACCOUNT_ERROR_RECORD_NOT_FOUND) {
6308 error_code = _account_type_query_by_app_id_from_global_db(app_id, account_type_record);
6311 _INFO("_account_type_query_by_app_id end [%d]", error_code);
6315 int _account_type_query_app_id_exist(const char* app_id)
6317 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
6320 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
6321 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
6323 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
6325 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
6326 rc = _account_get_record_count(query);
6328 if( _account_db_err_code() == SQLITE_PERM ){
6329 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
6330 return ACCOUNT_ERROR_PERMISSION_DENIED;
6334 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
6337 return ACCOUNT_ERROR_NONE;
6340 int _account_type_query_app_id_exist_from_all_db(const char *app_id)
6342 _INFO("_account_type_query_app_id_exist_from_all_db start app_id=%s", app_id);
6343 int return_code = ACCOUNT_ERROR_NONE;
6345 return_code = _account_type_query_app_id_exist(app_id);
6347 if (return_code == ACCOUNT_ERROR_RECORD_NOT_FOUND) {
6348 return_code = _account_type_query_app_id_exist_from_global_db(app_id);
6352 _INFO("_account_type_query_app_id_exist_from_all_db end");
6356 int _account_type_query_by_provider_feature_from_global_db(const char* key, GSList **account_type_list_all)
6358 int error_code = ACCOUNT_ERROR_NONE;
6359 account_stmt hstmt = NULL;
6360 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
6362 GSList *account_type_list = NULL;
6364 _INFO("_account_type_query_by_provider_feature_from_global_db start key=%s", key);
6367 ACCOUNT_ERROR("capability_type IS NULL.");
6368 error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
6372 if(g_hAccountGlobalDB == NULL)
6374 ACCOUNT_ERROR("The database isn't connected.");
6375 error_code = ACCOUNT_ERROR_DB_NOT_OPENED;
6379 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
6381 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
6383 hstmt = _account_prepare_query_from_global_db(query);
6385 if( _account_db_err_code_from_global_db() == SQLITE_PERM )
6387 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg_from_global_db());
6388 error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
6392 int binding_count = 1;
6393 _account_query_bind_text(hstmt, binding_count++, key);
6395 rc = _account_query_step(hstmt);
6397 account_type_s *account_type_record = NULL;
6399 if(rc != SQLITE_ROW)
6401 ACCOUNT_ERROR("The record isn't found. rc=[%d]", rc);
6402 error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
6406 while(rc == SQLITE_ROW) {
6407 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
6409 if (account_type_record == NULL) {
6410 ACCOUNT_FATAL("malloc Failed");
6414 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
6415 _account_type_convert_column_to_account_type(hstmt, account_type_record);
6416 account_type_list = g_slist_append(account_type_list, account_type_record);
6417 rc = _account_query_step(hstmt);
6420 rc = _account_query_finalize_from_global_db(hstmt);
6421 if (rc != ACCOUNT_ERROR_NONE )
6423 _account_type_gslist_account_type_free(account_type_list);
6424 ACCOUNT_ERROR("finalize error(%s)", rc);
6432 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
6433 account_type_s *account_type = NULL;
6434 account_type = (account_type_s*)iter->data;
6435 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
6436 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
6437 _INFO("add label & provider_feature");
6440 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
6442 account_type_s *account_type = NULL;
6443 account_type = (account_type_s*)iter->data;
6444 *account_type_list_all = g_slist_append(*account_type_list_all, account_type);
6445 _INFO("add account_type");
6448 error_code = ACCOUNT_ERROR_NONE;
6451 if (hstmt != NULL) {
6452 rc = _account_query_finalize_from_global_db(hstmt);
6453 if (rc != ACCOUNT_ERROR_NONE)
6455 ACCOUNT_ERROR("finalize error(%s)", rc);
6461 _INFO("_account_type_query_by_provider_feature_from_global_db end. error_code=[%d]", error_code);
6465 GSList* _account_type_query_by_provider_feature(const char* key, int *error_code)
6467 *error_code = ACCOUNT_ERROR_NONE;
6468 account_stmt hstmt = NULL;
6469 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
6471 GSList *account_type_list = NULL;
6473 _INFO("account_type_query_by_provider_feature start key=%s", key);
6476 ACCOUNT_ERROR("capability_type IS NULL.");
6477 *error_code = ACCOUNT_ERROR_INVALID_PARAMETER;
6481 if(g_hAccountDB == NULL)
6483 ACCOUNT_ERROR("The database isn't connected.");
6484 *error_code = ACCOUNT_ERROR_DB_NOT_OPENED;
6488 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
6490 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId IN (SELECT app_id from %s WHERE key=?)", ACCOUNT_TYPE_TABLE, PROVIDER_FEATURE_TABLE);
6492 hstmt = _account_prepare_query(query);
6494 if( _account_db_err_code() == SQLITE_PERM )
6496 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
6497 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
6501 int binding_count = 1;
6502 _account_query_bind_text(hstmt, binding_count++, key);
6504 rc = _account_query_step(hstmt);
6506 account_type_s *account_type_record = NULL;
6508 if(rc != SQLITE_ROW)
6510 ACCOUNT_ERROR("The record isn't found. rc=[%d]", rc);
6511 *error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
6515 while(rc == SQLITE_ROW) {
6516 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
6518 if (account_type_record == NULL) {
6519 ACCOUNT_FATAL("malloc Failed");
6523 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
6524 _account_type_convert_column_to_account_type(hstmt, account_type_record);
6525 account_type_list = g_slist_append(account_type_list, account_type_record);
6526 rc = _account_query_step(hstmt);
6529 rc = _account_query_finalize(hstmt);
6530 if (rc != ACCOUNT_ERROR_NONE )
6532 _account_type_gslist_account_type_free(account_type_list);
6533 ACCOUNT_ERROR("finalize error(%s)", rc);
6541 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
6542 account_type_s *account_type = NULL;
6543 account_type = (account_type_s*)iter->data;
6544 _account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
6545 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
6548 *error_code = ACCOUNT_ERROR_NONE;
6551 if (hstmt != NULL) {
6552 rc = _account_query_finalize(hstmt);
6553 if (rc != ACCOUNT_ERROR_NONE)
6561 if (*error_code == ACCOUNT_ERROR_NONE || *error_code == ACCOUNT_ERROR_RECORD_NOT_FOUND) {
6562 rc = _account_type_query_by_provider_feature_from_global_db(key, &account_type_list);
6563 if (rc != ACCOUNT_ERROR_NONE && rc != ACCOUNT_ERROR_RECORD_NOT_FOUND) {
6564 ACCOUNT_ERROR( "_account_type_query_by_provider_feature_from_global_db fail=[%d]", rc);
6565 _account_type_gslist_account_type_free(account_type_list);
6568 if (rc == ACCOUNT_ERROR_NONE)
6572 _INFO("account_type_query_by_provider_feature end");
6573 return account_type_list;
6576 int _account_type_query_all_from_global_db(GSList **account_type_list_all)
6578 account_stmt hstmt = NULL;
6579 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
6580 int rc = ACCOUNT_ERROR_NONE;
6581 int error_code = ACCOUNT_ERROR_NONE;
6582 GSList *account_type_list = NULL;
6584 _INFO("_account_type_query_all_in_global_db start");
6585 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, NULL, ("The database isn't connected."));
6587 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
6589 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
6590 hstmt = _account_prepare_query_from_global_db(query);
6592 rc = _account_query_step(hstmt);
6594 if( _account_db_err_code_from_global_db() == SQLITE_PERM ){
6595 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg_from_global_db());
6596 return ACCOUNT_ERROR_PERMISSION_DENIED;
6599 account_type_s *account_type_record = NULL;
6601 if (rc != SQLITE_ROW)
6603 _INFO("[ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found.");
6604 error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
6608 while(rc == SQLITE_ROW) {
6609 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
6611 if (account_type_record == NULL) {
6612 ACCOUNT_FATAL("malloc Failed");
6616 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
6617 _account_type_convert_column_to_account_type(hstmt, account_type_record);
6618 account_type_list = g_slist_append(account_type_list, account_type_record);
6619 rc = _account_query_step(hstmt);
6622 rc = _account_query_finalize_from_global_db(hstmt);
6623 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6628 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
6629 account_type_s *account_type = NULL;
6630 account_type = (account_type_s*)iter->data;
6631 _account_type_query_label_by_app_id_from_global_db(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
6632 _account_type_query_provider_feature_cb_by_app_id_from_global_db(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
6635 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
6636 account_type_s *account_type = NULL;
6637 account_type = (account_type_s*)iter->data;
6638 *account_type_list_all = g_slist_append(*account_type_list_all, account_type);
6641 error_code = ACCOUNT_ERROR_NONE;
6645 rc = _account_query_finalize_from_global_db(hstmt);
6646 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list);}, rc, ("finalize error"));
6650 _INFO("_account_type_query_all_in_global_db end");
6654 GSList* _account_type_query_all(void)
6656 account_stmt hstmt = NULL;
6657 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
6659 int error_code = ACCOUNT_ERROR_NONE;
6660 GSList *account_type_list = NULL;
6662 _INFO("_account_type_query_all start");
6663 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, NULL, ("The database isn't connected."));
6665 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
6667 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
6668 hstmt = _account_prepare_query(query);
6670 rc = _account_query_step(hstmt);
6672 if( _account_db_err_code() == SQLITE_PERM ){
6673 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
6677 account_type_s *account_type_record = NULL;
6679 if (rc != SQLITE_ROW)
6681 _INFO("[ACCOUNT_ERROR_RECORD_NOT_FOUND]The record isn't found.");
6682 error_code = ACCOUNT_ERROR_RECORD_NOT_FOUND;
6686 while(rc == SQLITE_ROW) {
6687 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
6689 if (account_type_record == NULL) {
6690 ACCOUNT_FATAL("malloc Failed");
6694 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
6695 _account_type_convert_column_to_account_type(hstmt, account_type_record);
6696 account_type_list = g_slist_append(account_type_list, account_type_record);
6697 rc = _account_query_step(hstmt);
6700 rc = _account_query_finalize(hstmt);
6701 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, NULL, ("finalize error"));
6706 for (iter = account_type_list; iter != NULL; iter = g_slist_next(iter)) {
6707 account_type_s *account_type = NULL;
6708 account_type = (account_type_s*)iter->data;
6709 _account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
6710 _account_type_query_provider_feature_cb_by_app_id(_account_get_provider_feature_cb, account_type->app_id,(void*)account_type);
6713 error_code = ACCOUNT_ERROR_NONE;
6717 rc = _account_query_finalize(hstmt);
6718 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {_account_type_gslist_account_type_free(account_type_list);}, NULL, ("finalize error"));
6722 if (error_code == ACCOUNT_ERROR_NONE || error_code == ACCOUNT_ERROR_RECORD_NOT_FOUND) {
6723 error_code = _account_type_query_all_from_global_db(&account_type_list);
6724 if (rc != ACCOUNT_ERROR_NONE && rc != ACCOUNT_ERROR_RECORD_NOT_FOUND) {
6725 ACCOUNT_ERROR( "_account_type_query_all_from_global_db fail=[%d]", rc);
6726 _account_type_gslist_account_type_free(account_type_list);
6731 _INFO("_account_type_query_all end");
6732 return account_type_list;
6735 // output parameter label must be free
6736 int _account_type_query_label_by_locale_from_global_db(const char* app_id, const char* locale, char **label)
6738 int error_code = ACCOUNT_ERROR_NONE;
6739 account_stmt hstmt = NULL;
6740 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
6741 int rc = 0, binding_count = 1;
6742 char* converted_locale = NULL;
6744 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
6745 ACCOUNT_RETURN_VAL((g_hAccountGlobalDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
6746 ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
6747 ACCOUNT_RETURN_VAL((locale != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null"));
6748 //Making label newly created
6750 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
6752 converted_locale = _account_get_text(locale);
6753 gchar** tokens = g_strsplit(converted_locale, "-", 2);
6755 if(tokens != NULL) {
6756 if((char*)(tokens[1]) != NULL) {
6757 char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
6758 if(upper_token != NULL) {
6759 _ACCOUNT_FREE(converted_locale);
6760 converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
6762 _ACCOUNT_FREE(upper_token);
6767 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale);
6768 _ACCOUNT_FREE(converted_locale);
6770 hstmt = _account_prepare_query_from_global_db(query);
6772 if( _account_db_err_code_from_global_db() == SQLITE_PERM ){
6773 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg_from_global_db());
6774 return ACCOUNT_ERROR_PERMISSION_DENIED;
6777 _account_query_bind_text(hstmt, binding_count++, app_id);
6779 rc = _account_query_step(hstmt);
6780 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
6782 label_s* label_record = NULL;
6784 while (rc == SQLITE_ROW) {
6785 label_record = (label_s*) malloc(sizeof(label_s));
6787 if (label_record == NULL) {
6788 ACCOUNT_FATAL("malloc Failed");
6792 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
6794 _account_type_convert_column_to_label(hstmt,label_record);
6796 _ACCOUNT_FREE(*label);
6797 //Making label newly created
6798 *label = _account_get_text(label_record->label);
6800 _account_type_free_label_with_items(label_record);
6802 rc = _account_query_step(hstmt);
6805 rc = _account_query_finalize_from_global_db(hstmt);
6806 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6809 error_code = ACCOUNT_ERROR_NONE;
6812 if (hstmt != NULL) {
6813 rc = _account_query_finalize_from_global_db(hstmt);
6814 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6818 _INFO("_account_type_query_label_by_locale_from_global_db() end : error_code = %d", error_code);
6822 // output parameter label must be free
6823 int _account_type_query_label_by_locale(const char* app_id, const char* locale, char **label)
6825 int error_code = ACCOUNT_ERROR_NONE;
6826 account_stmt hstmt = NULL;
6827 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
6828 int rc = 0, binding_count = 1;
6829 char* converted_locale = NULL;
6831 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
6832 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
6833 ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
6834 ACCOUNT_RETURN_VAL((locale != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("locale char is null"));
6835 //Making label newly created
6837 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
6839 converted_locale = _account_get_text(locale);
6840 gchar** tokens = g_strsplit(converted_locale, "-", 2);
6842 if(tokens != NULL) {
6843 if((char*)(tokens[1]) != NULL) {
6844 char* upper_token = g_ascii_strup(tokens[1], strlen(tokens[1]));
6845 if(upper_token != NULL) {
6846 _ACCOUNT_FREE(converted_locale);
6847 converted_locale = g_strdup_printf("%s_%s", tokens[0], upper_token);
6849 _ACCOUNT_FREE(upper_token);
6854 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, converted_locale);
6855 _ACCOUNT_FREE(converted_locale);
6857 hstmt = _account_prepare_query(query);
6859 if( _account_db_err_code() == SQLITE_PERM ){
6860 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
6861 return ACCOUNT_ERROR_PERMISSION_DENIED;
6864 _account_query_bind_text(hstmt, binding_count++, app_id);
6866 rc = _account_query_step(hstmt);
6867 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
6869 label_s* label_record = NULL;
6871 while (rc == SQLITE_ROW) {
6872 label_record = (label_s*) malloc(sizeof(label_s));
6874 if (label_record == NULL) {
6875 ACCOUNT_FATAL("malloc Failed");
6879 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
6881 _account_type_convert_column_to_label(hstmt,label_record);
6883 _ACCOUNT_FREE(*label);
6884 //Making label newly created
6885 *label = _account_get_text(label_record->label);
6887 _account_type_free_label_with_items(label_record);
6889 rc = _account_query_step(hstmt);
6892 rc = _account_query_finalize(hstmt);
6893 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6896 error_code = ACCOUNT_ERROR_NONE;
6899 if (hstmt != NULL) {
6900 rc = _account_query_finalize(hstmt);
6901 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6905 if (error_code == ACCOUNT_ERROR_RECORD_NOT_FOUND) {
6906 error_code = _account_type_query_label_by_locale_from_global_db(app_id, locale, label);
6909 _INFO("_account_type_query_label_by_locale() end : error_code = %d", error_code);
6913 static int _account_insert_custom(account_s *account, int account_id)
6915 _INFO("_account_insert_custom start");
6918 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
6919 account_stmt hstmt = NULL;
6921 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
6923 if (g_slist_length( account->custom_list)==0) {
6924 ACCOUNT_DEBUG( "_account_insert_custom, no custom data\n");
6925 return ACCOUNT_ERROR_NONE;
6928 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
6930 rc = _account_get_record_count(query);
6932 if( _account_db_err_code() == SQLITE_PERM ){
6933 ACCOUNT_ERROR( "Access failed(%d, %s)", _account_db_err_msg());
6934 return ACCOUNT_ERROR_PERMISSION_DENIED;
6938 ACCOUNT_SLOGE( "_account_insert_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
6939 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
6946 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
6949 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
6950 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (AccountId, AppId, Key, Value) VALUES "
6951 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
6953 hstmt = _account_prepare_query(query);
6955 if( _account_db_err_code() == SQLITE_PERM ){
6956 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
6957 return ACCOUNT_ERROR_PERMISSION_DENIED;
6960 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
6962 account_custom_s* custom_data = NULL;
6963 custom_data = (account_custom_s*)iter->data;
6965 ret = _account_query_bind_int(hstmt, count++, account_id);
6966 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
6967 ret = _account_query_bind_text(hstmt, count++, account->package_name);
6968 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
6969 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
6970 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
6971 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
6972 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
6974 rc = _account_query_step(hstmt);
6976 if (rc != SQLITE_DONE) {
6977 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
6981 rc = _account_query_finalize(hstmt);
6982 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
6987 _INFO("_account_insert_custom end");
6988 return ACCOUNT_ERROR_NONE;
6991 static int _account_update_custom(account_s *account, int account_id)
6994 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
6995 account_stmt hstmt = NULL;
6997 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
6999 if (g_slist_length( account->custom_list)==0) {
7000 ACCOUNT_DEBUG( "_account_update_custom, no custom data\n");
7001 return ACCOUNT_ERROR_NONE;
7004 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
7006 rc = _account_get_record_count(query);
7008 if( _account_db_err_code() == SQLITE_PERM ){
7009 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
7010 pthread_mutex_unlock(&account_mutex);
7011 return ACCOUNT_ERROR_PERMISSION_DENIED;
7012 } else if( _account_db_err_code() == SQLITE_BUSY ){
7013 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
7014 pthread_mutex_unlock(&account_mutex);
7015 return ACCOUNT_ERROR_DATABASE_BUSY;
7019 ACCOUNT_SLOGE( "_account_update_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
7020 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
7023 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
7025 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId=? ", ACCOUNT_CUSTOM_TABLE);
7026 hstmt = _account_prepare_query(query);
7028 _account_query_bind_int(hstmt, count++, (int)account_id);
7029 rc = _account_query_step(hstmt);
7031 if (rc == SQLITE_BUSY) {
7032 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
7033 return ACCOUNT_ERROR_DATABASE_BUSY;
7034 } else if (rc != SQLITE_DONE) {
7035 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
7036 return ACCOUNT_ERROR_DB_FAILED;
7039 rc = _account_query_finalize(hstmt);
7040 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
7045 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
7048 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
7049 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AccountId, AppId, Key, Value) VALUES "
7050 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
7052 hstmt = _account_prepare_query(query);
7054 if( _account_db_err_code() == SQLITE_PERM ){
7055 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
7056 return ACCOUNT_ERROR_PERMISSION_DENIED;
7059 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
7061 account_custom_s* custom_data = NULL;
7062 custom_data = (account_custom_s*)iter->data;
7064 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
7065 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
7066 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
7067 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
7068 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
7069 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
7070 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
7071 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
7073 rc = _account_query_step(hstmt);
7075 if (rc != SQLITE_DONE) {
7076 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
7080 rc = _account_query_finalize(hstmt);
7081 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
7086 return ACCOUNT_ERROR_NONE;
7089 static int _account_query_custom_by_account_id(account_custom_cb callback, int account_id, void *user_data )
7091 int error_code = ACCOUNT_ERROR_NONE;
7092 account_stmt hstmt = NULL;
7093 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
7096 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
7097 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
7098 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
7100 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
7102 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
7103 hstmt = _account_prepare_query(query);
7105 if( _account_db_err_code() == SQLITE_PERM ){
7106 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
7107 return ACCOUNT_ERROR_PERMISSION_DENIED;
7110 rc = _account_query_step(hstmt);
7112 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
7114 account_custom_s* custom_record = NULL;
7116 while (rc == SQLITE_ROW) {
7117 bool cb_ret = FALSE;
7118 custom_record = (account_custom_s*) malloc(sizeof(account_custom_s));
7120 if (custom_record == NULL) {
7121 ACCOUNT_FATAL("malloc Failed");
7125 ACCOUNT_MEMSET(custom_record, 0x00, sizeof(account_custom_s));
7127 _account_convert_column_to_custom(hstmt, custom_record);
7129 cb_ret = callback(custom_record->key, custom_record->value, user_data);
7131 _account_free_custom_with_items(custom_record);
7133 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
7135 rc = _account_query_step(hstmt);
7138 rc = _account_query_finalize(hstmt);
7139 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
7142 error_code = ACCOUNT_ERROR_NONE;
7145 if (hstmt != NULL) {
7146 rc = _account_query_finalize(hstmt);
7147 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
7151 pthread_mutex_unlock(&account_mutex);