3 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
28 #include <account-private.h>
29 #include <account_free.h>
31 #include "account_private_client.h"
32 #include "account_internal.h"
34 #ifdef TIZEN_PROFILE_MOBILE
35 #include "mobile/account.h"
37 #include "wearable/account.h"
40 #define ACCOUNT_DB_OPEN_READONLY 0
41 #define ACCOUNT_DB_OPEN_READWRITE 1
43 typedef sqlite3_stmt* account_stmt;
45 static sqlite3* g_hAccountDB = NULL;
46 static int g_refCntDB = 0;
47 pthread_mutex_t account_mutex = PTHREAD_MUTEX_INITIALIZER;
49 static char *_account_get_text(const char *text_data)
51 char *text_value = NULL;
53 if (text_data != NULL) {
54 text_value = strdup(text_data);
59 static const char *_account_db_err_msg()
61 return sqlite3_errmsg(g_hAccountDB);
64 static int _account_db_err_code()
66 return sqlite3_errcode(g_hAccountDB);
69 static int _account_db_open(int mode)
73 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
75 char *client_db_path = NULL;
77 _INFO( "start to get DB path");
79 ret = vsm_canonicalize_path("/opt/usr/dbspace/.account.db", &client_db_path);
81 _ERR( "vsm_canonicalize_path fail ret = %d", ret);
82 _ACCOUNT_FREE(client_db_path);
83 return ACCOUNT_ERROR_PERMISSION_DENIED;
86 _INFO( "account_db_path canonicalized = %s", client_db_path);
88 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
91 if(mode == ACCOUNT_DB_OPEN_READWRITE)
92 rc = db_util_open(client_db_path, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD);
93 else if(mode == ACCOUNT_DB_OPEN_READONLY)
94 rc = db_util_open_with_options(client_db_path, &g_hAccountDB, SQLITE_OPEN_READONLY, NULL);
96 _ACCOUNT_FREE(client_db_path);
97 return ACCOUNT_ERROR_DB_NOT_OPENED;
99 _ACCOUNT_FREE(client_db_path);
101 if( _account_db_err_code() == SQLITE_PERM ){
102 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
103 return ACCOUNT_ERROR_PERMISSION_DENIED;
106 ACCOUNT_RETURN_VAL((rc != SQLITE_PERM), {}, ACCOUNT_ERROR_PERMISSION_DENIED, ("Account permission denied rc : %d", rc));
107 ACCOUNT_RETURN_VAL((rc == SQLITE_OK), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", rc));
111 _ACCOUNT_FREE(client_db_path);
114 ACCOUNT_RETURN_VAL((rc == SQLITE_OK), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("busy handler fail. rc : %d", rc));
116 return ACCOUNT_ERROR_NONE;
119 static int _account_db_close(void)
125 if (g_refCntDB > 0) {
128 if (g_refCntDB == 0) {
129 rc = db_util_close(g_hAccountDB);
130 if( rc == SQLITE_PERM ){
131 ACCOUNT_ERROR( "Access failed(SQLITE_PERM)");
132 return ACCOUNT_ERROR_PERMISSION_DENIED;
133 } else if ( rc == SQLITE_BUSY ){
134 ACCOUNT_ERROR( "database busy");
135 return ACCOUNT_ERROR_DATABASE_BUSY;
137 ACCOUNT_RETURN_VAL((rc == SQLITE_OK), {}, ACCOUNT_ERROR_DB_FAILED, ("The database isn't connected. rc : %d", rc));
140 ret = ACCOUNT_ERROR_NONE;
142 ACCOUNT_ERROR( "_account_svc_db_close: No handle(). refcnt=%d ", g_refCntDB);
143 ret = ACCOUNT_ERROR_DB_FAILED;
149 static int _account_execute_query(const char *query)
152 char* pszErrorMsg = NULL;
155 ACCOUNT_ERROR("NULL query\n");
156 return ACCOUNT_ERROR_QUERY_SYNTAX_ERROR;
160 ACCOUNT_ERROR("DB is not opened\n");
161 return ACCOUNT_ERROR_DB_NOT_OPENED;
164 rc = sqlite3_exec(g_hAccountDB, query, NULL, NULL, &pszErrorMsg);
165 if (SQLITE_OK != rc) {
166 ACCOUNT_ERROR("sqlite3_exec rc(%d) query(%s) failed(%s).", rc, query, pszErrorMsg);
167 sqlite3_free(pszErrorMsg);
173 static int _account_begin_transaction(void)
175 ACCOUNT_DEBUG("_account_begin_transaction start");
178 ret = _account_execute_query("BEGIN IMMEDIATE TRANSACTION");
180 if (ret == SQLITE_BUSY){
181 ACCOUNT_ERROR(" sqlite3 busy = %d", ret);
182 return ACCOUNT_ERROR_DATABASE_BUSY;
183 } else if(ret != SQLITE_OK) {
184 ACCOUNT_ERROR("_account_svc_begin_transaction fail :: %d", ret);
185 return ACCOUNT_ERROR_DB_FAILED;
188 ACCOUNT_DEBUG("_account_begin_transaction end");
189 return ACCOUNT_ERROR_NONE;
192 static int _account_end_transaction(bool is_success)
194 ACCOUNT_DEBUG("_account_end_transaction start");
198 if (is_success == true) {
199 ret = _account_execute_query("COMMIT TRANSACTION");
200 ACCOUNT_DEBUG("_account_end_transaction COMMIT");
202 ret = _account_execute_query("ROLLBACK TRANSACTION");
203 ACCOUNT_DEBUG("_account_end_transaction ROLLBACK");
206 if(ret == SQLITE_PERM){
207 ACCOUNT_ERROR("Account permission denied :: %d", ret);
208 return ACCOUNT_ERROR_PERMISSION_DENIED;
211 if (ret == SQLITE_BUSY){
212 ACCOUNT_DEBUG(" sqlite3 busy = %d", ret);
213 return ACCOUNT_ERROR_DATABASE_BUSY;
216 if (ret != SQLITE_OK) {
217 ACCOUNT_ERROR("_account_svc_end_transaction fail :: %d", ret);
218 return ACCOUNT_ERROR_DB_FAILED;
221 ACCOUNT_DEBUG("_account_end_transaction end");
222 return ACCOUNT_ERROR_NONE;
225 static int _account_get_record_count(char* query)
227 _INFO("_account_get_record_count");
231 account_stmt pStmt = NULL;
234 _ERR("NULL query\n");
235 return ACCOUNT_ERROR_QUERY_SYNTAX_ERROR;
239 _ERR("DB is not opened\n");
240 return ACCOUNT_ERROR_DB_NOT_OPENED;
243 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
245 if (SQLITE_BUSY == rc){
246 _ERR("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg());
247 sqlite3_finalize(pStmt);
248 return ACCOUNT_ERROR_DATABASE_BUSY;
249 } else if (SQLITE_OK != rc) {
250 _ERR("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg());
251 sqlite3_finalize(pStmt);
252 return ACCOUNT_ERROR_DB_FAILED;
255 rc = sqlite3_step(pStmt);
256 if (SQLITE_BUSY == rc) {
257 _ERR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg());
258 sqlite3_finalize(pStmt);
259 return ACCOUNT_ERROR_DATABASE_BUSY;
260 } else if (SQLITE_ROW != rc) {
261 _ERR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg());
262 sqlite3_finalize(pStmt);
263 return ACCOUNT_ERROR_DB_FAILED;
266 ncount = sqlite3_column_int(pStmt, 0);
268 _INFO("account record count [%d]", ncount);
269 sqlite3_finalize(pStmt);
274 static int _account_query_bind_int(account_stmt pStmt, int pos, int num)
277 ACCOUNT_ERROR("statement is null");
282 ACCOUNT_ERROR("invalid pos");
286 return sqlite3_bind_int(pStmt, pos, num);
289 static int _account_query_bind_text(account_stmt pStmt, int pos, const char *str)
291 _INFO("_account_query_bind_text");
295 _ERR("statement is null");
301 _INFO("sqlite3_bind_text");
302 return sqlite3_bind_text(pStmt, pos, (const char*)str, strlen(str), SQLITE_STATIC);
306 _INFO("sqlite3_bind_null");
307 return sqlite3_bind_null(pStmt, pos);
311 static int _account_type_convert_account_to_sql(account_type_s *account_type, account_stmt hstmt, char *sql_value)
317 /*Caution : Keep insert query orders.*/
320 _account_query_bind_text(hstmt, count++, (char*)account_type->app_id);
322 /* 2. service provider id*/
323 _account_query_bind_text(hstmt, count++, (char*)account_type->service_provider_id);
326 _account_query_bind_text(hstmt, count++, (char*)account_type->icon_path);
328 /* 4. small icon path*/
329 _account_query_bind_text(hstmt, count++, (char*)account_type->small_icon_path);
331 /* 5. multiple accont support*/
332 _account_query_bind_int(hstmt, count++, account_type->multiple_account_support);
339 static gboolean _account_type_check_duplicated(account_type_s *data)
341 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
344 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
346 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId='%s'"
347 , ACCOUNT_TYPE_TABLE, data->app_id);
349 count = _account_get_record_count(query);
357 static int _account_query_finalize(account_stmt pStmt)
362 ACCOUNT_ERROR( "pStmt is NULL");
363 return ACCOUNT_ERROR_INVALID_PARAMETER;
366 rc = sqlite3_finalize(pStmt);
367 if (rc == SQLITE_BUSY){
368 ACCOUNT_ERROR(" sqlite3 busy = %d", rc);
369 return ACCOUNT_ERROR_DATABASE_BUSY;
370 } else if (rc != SQLITE_OK) {
371 ACCOUNT_ERROR( "sqlite3_finalize fail, rc : %d, db_error : %s\n", rc, _account_db_err_msg());
372 return ACCOUNT_ERROR_DB_FAILED;
375 return ACCOUNT_ERROR_NONE;
378 static int _account_query_step(account_stmt pStmt)
381 ACCOUNT_ERROR( "pStmt is NULL");
385 return sqlite3_step(pStmt);
388 static account_stmt _account_prepare_query(char *query)
391 account_stmt pStmt = NULL;
393 ACCOUNT_RETURN_VAL((query != NULL), {}, NULL, ("query is NULL"));
395 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
397 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, NULL, ("sqlite3_prepare_v2(%s) failed(%s).", query, _account_db_err_msg()));
402 static int _account_get_next_sequence(const char *pszName)
405 account_stmt pStmt = NULL;
407 char szQuery[ACCOUNT_SQL_LEN_MAX] = {0,};
409 ACCOUNT_MEMSET(szQuery, 0x00, sizeof(szQuery));
410 ACCOUNT_SNPRINTF(szQuery, sizeof(szQuery), "SELECT max(seq) FROM %s where name = '%s' ", ACCOUNT_SQLITE_SEQ, pszName);
411 rc = sqlite3_prepare_v2(g_hAccountDB, szQuery, strlen(szQuery), &pStmt, NULL);
412 if (SQLITE_OK != rc) {
413 ACCOUNT_SLOGE("sqlite3_prepare_v2() failed(%d, %s).", rc, _account_db_err_msg());
414 sqlite3_finalize(pStmt);
415 return ACCOUNT_ERROR_DB_FAILED;
418 rc = sqlite3_step(pStmt);
419 max_seq = sqlite3_column_int(pStmt, 0);
422 /*Finalize Statement*/
423 rc = sqlite3_finalize(pStmt);
429 static int _account_type_insert_provider_feature(account_type_s *account_type, const char* app_id)
432 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
433 account_stmt hstmt = NULL;
435 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
436 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
438 if (g_slist_length( account_type->provider_feature_list)==0) {
439 ACCOUNT_ERROR( "no capability\n");
440 return ACCOUNT_ERROR_NONE;
443 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId='%s'", ACCOUNT_TYPE_TABLE, app_id);
445 rc = _account_get_record_count(query);
447 if( _account_db_err_code() == SQLITE_PERM ){
448 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
449 return ACCOUNT_ERROR_PERMISSION_DENIED;
453 ACCOUNT_SLOGI( "related account type item is not existed rc=%d , %s", rc, _account_db_err_msg());
454 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
461 for (iter = account_type->provider_feature_list; iter != NULL; iter = g_slist_next(iter)) {
464 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
465 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(app_id, key) VALUES "
466 "(?, ?) ", PROVIDER_FEATURE_TABLE);
468 hstmt = _account_prepare_query(query);
470 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
472 provider_feature_s* feature_data = NULL;
473 feature_data = (provider_feature_s*)iter->data;
475 ret = _account_query_bind_text(hstmt, count++, app_id);
476 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
477 ret = _account_query_bind_text(hstmt, count++, feature_data->key);
478 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
480 rc = _account_query_step(hstmt);
482 if (rc != SQLITE_DONE) {
483 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
487 rc = _account_query_finalize(hstmt);
488 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
493 return ACCOUNT_ERROR_NONE;
496 static int _account_type_insert_label(account_type_s *account_type)
499 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
500 account_stmt hstmt = NULL;
502 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
504 if (g_slist_length( account_type->label_list)==0) {
505 ACCOUNT_ERROR( "_account_type_insert_label, no label\n");
506 return ACCOUNT_ERROR_NONE;
509 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId = '%s'", ACCOUNT_TYPE_TABLE, account_type->app_id);
511 rc = _account_get_record_count(query);
513 if( _account_db_err_code() == SQLITE_PERM ){
514 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
515 return ACCOUNT_ERROR_PERMISSION_DENIED;
519 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
525 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
528 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
529 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
530 "(?, ?, ?) ", LABEL_TABLE);
532 hstmt = _account_prepare_query(query);
534 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
536 label_s* label_data = NULL;
537 label_data = (label_s*)iter->data;
539 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
540 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
541 ret = _account_query_bind_text(hstmt, count++, label_data->label);
542 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
543 ret = _account_query_bind_text(hstmt, count++, (char*)label_data->locale);
544 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
546 rc = _account_query_step(hstmt);
548 if (rc != SQLITE_DONE) {
549 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
553 rc = _account_query_finalize(hstmt);
554 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
559 return ACCOUNT_ERROR_NONE;
562 static int _account_type_execute_insert_query(account_type_s *account_type)
567 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
568 int error_code = ACCOUNT_ERROR_NONE;
569 account_stmt hstmt = NULL;
571 /* check mandatory field */
572 // app id & service provider id
573 if (!account_type->app_id) {
574 return ACCOUNT_ERROR_INVALID_PARAMETER;
577 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
578 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s( AppId, ServiceProviderId , IconPath , SmallIconPath , MultipleAccountSupport ) values "
579 "(?, ?, ?, ?, ?)", ACCOUNT_TYPE_TABLE);
582 hstmt = _account_prepare_query(query);
585 if( _account_db_err_code() == SQLITE_PERM ){
586 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
587 return ACCOUNT_ERROR_PERMISSION_DENIED;
588 } else if( _account_db_err_code() == SQLITE_BUSY ){
589 ACCOUNT_ERROR( "Database Busy(%s)", _account_db_err_msg());
590 return ACCOUNT_ERROR_DATABASE_BUSY;
593 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
596 _account_type_convert_account_to_sql(account_type, hstmt, query);
599 rc = _account_query_step(hstmt);
600 if (rc == SQLITE_BUSY) {
601 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
602 error_code = ACCOUNT_ERROR_DATABASE_BUSY;
603 } else if (rc != SQLITE_DONE) {
604 ACCOUNT_ERROR( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
605 error_code = ACCOUNT_ERROR_DB_FAILED;
609 rc = _account_query_finalize(hstmt);
610 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
617 int _account_type_insert_to_db(account_type_s* account_type, int* account_type_id)
621 int error_code = ACCOUNT_ERROR_NONE, ret_transaction = 0;
623 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
624 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL"));
625 ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID POINTER IS NULL"));
627 account_type_s *data = (account_type_s*)account_type;
629 pthread_mutex_lock(&account_mutex);
632 /* transaction control required*/
633 ret_transaction = _account_begin_transaction();
637 if( _account_db_err_code() == SQLITE_PERM ){
638 pthread_mutex_unlock(&account_mutex);
639 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
640 return ACCOUNT_ERROR_PERMISSION_DENIED;
644 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
645 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
646 pthread_mutex_unlock(&account_mutex);
647 return ACCOUNT_ERROR_DATABASE_BUSY;
648 } else if (ret_transaction != ACCOUNT_ERROR_NONE) {
649 ACCOUNT_ERROR("_account_begin_transaction fail %d\n", ret_transaction);
650 pthread_mutex_unlock(&account_mutex);
651 return ret_transaction;
655 if (_account_type_check_duplicated(data)) {
657 ret_transaction = _account_end_transaction(FALSE);
658 ACCOUNT_ERROR("Duplicated, rollback insert query(%x)!!!!\n", ret_transaction);
659 *account_type_id = -1;
660 pthread_mutex_unlock(&account_mutex);
661 return ACCOUNT_ERROR_DUPLICATED;
664 *account_type_id = _account_get_next_sequence(ACCOUNT_TYPE_TABLE);
666 error_code = _account_type_execute_insert_query(data);
668 if (error_code != ACCOUNT_ERROR_NONE){
669 error_code = ACCOUNT_ERROR_DUPLICATED;
670 ret_transaction = _account_end_transaction(FALSE);
671 ACCOUNT_ERROR("Insert fail, rollback insert query(%x)!!!!\n", ret_transaction);
672 *account_type_id = -1;
673 pthread_mutex_unlock(&account_mutex);
679 error_code = _account_type_insert_provider_feature(data, data->app_id);
680 if(error_code != ACCOUNT_ERROR_NONE) {
682 ret_transaction = _account_end_transaction(FALSE);
683 ACCOUNT_ERROR("Insert provider feature fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
684 pthread_mutex_unlock(&account_mutex);
688 error_code = _account_type_insert_label(data);
689 if(error_code != ACCOUNT_ERROR_NONE) {
691 ret_transaction = _account_end_transaction(FALSE);
692 ACCOUNT_ERROR("Insert label fail(%x), rollback insert query(%x)!!!!\n", error_code, ret_transaction);
693 pthread_mutex_unlock(&account_mutex);
697 ret_transaction = _account_end_transaction(TRUE);
699 pthread_mutex_unlock(&account_mutex);
702 return ACCOUNT_ERROR_NONE;
705 int _account_type_delete_by_app_id(const char* app_id)
707 int error_code = ACCOUNT_ERROR_NONE;
708 account_stmt hstmt = NULL;
709 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
710 int rc = 0, count = -1;
711 int ret_transaction = 0;
712 int binding_count = 1;
713 bool is_success = FALSE;
715 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
716 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("The database isn't connected."));
718 /* Check requested ID to delete */
719 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
721 count = _account_get_record_count(query);
723 if( _account_db_err_code() == SQLITE_PERM ){
724 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
725 return ACCOUNT_ERROR_PERMISSION_DENIED;
729 ACCOUNT_SLOGE("app id(%s) is not exist. count(%d)\n", app_id, count);
730 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
733 /* transaction control required*/
734 ret_transaction = _account_begin_transaction();
736 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
737 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
738 pthread_mutex_unlock(&account_mutex);
739 return ACCOUNT_ERROR_DATABASE_BUSY;
740 }else if (ret_transaction != ACCOUNT_ERROR_NONE) {
741 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
742 pthread_mutex_unlock(&account_mutex);
743 return ret_transaction;
746 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", LABEL_TABLE);
748 hstmt = _account_prepare_query(query);
750 if( _account_db_err_code() == SQLITE_PERM ){
751 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
752 pthread_mutex_unlock(&account_mutex);
753 return ACCOUNT_ERROR_PERMISSION_DENIED;
756 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
757 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
759 _account_query_bind_text(hstmt, binding_count++, app_id);
761 rc = _account_query_step(hstmt);
762 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
764 rc = _account_query_finalize(hstmt);
765 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
769 ACCOUNT_MEMSET(query, 0, sizeof(query));
771 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE app_id = ? ", PROVIDER_FEATURE_TABLE);
773 hstmt = _account_prepare_query(query);
774 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
775 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
777 _account_query_bind_text(hstmt, binding_count++, app_id);
779 rc = _account_query_step(hstmt);
780 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
782 rc = _account_query_finalize(hstmt);
783 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
789 ACCOUNT_MEMSET(query, 0, sizeof(query));
791 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ? ", ACCOUNT_TYPE_TABLE);
793 hstmt = _account_prepare_query(query);
794 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
795 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
797 _account_query_bind_text(hstmt, binding_count++, app_id);
799 rc = _account_query_step(hstmt);
800 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
802 rc = _account_query_finalize(hstmt);
803 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
810 rc = _account_query_finalize(hstmt);
811 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
815 ret_transaction = _account_end_transaction(is_success);
817 if (ret_transaction != ACCOUNT_ERROR_NONE) {
818 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
821 pthread_mutex_unlock(&account_mutex);
826 ACCOUNT_INTERNAL_API int account_type_insert_to_db_offline(account_type_h account_type, int* account_type_id)
828 _INFO("account_type_insert_to_db starting");
830 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL"));
831 ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID POINTER IS NULL"));
835 _INFO("account_manager_account_type_add start");
837 guint pid = getpid();
838 _INFO("client Id = [%u]", pid);
840 int return_code = _account_db_open(1);
841 if (return_code != ACCOUNT_ERROR_NONE)
843 _ERR("_account_db_open() error, ret = %d", return_code);
851 _ERR("current daemon is not root user, uid=%d", uid);
855 _INFO("before _account_type_insert_to_db");
856 return_code = _account_type_insert_to_db((account_type_s*)account_type, &db_id);
857 _INFO("after _account_type_insert_to_db");
858 if (return_code != ACCOUNT_ERROR_NONE)
860 _ERR("_account_type_insert_to_db error");
864 *account_type_id = db_id;
866 account_type_s* account_type_data = (account_type_s*)account_type;
867 account_type_data->id = db_id;
870 _INFO("account_manager_account_type_add end");
872 if( g_hAccountDB == NULL )
875 return_code = _account_db_close();
876 if (return_code != ACCOUNT_ERROR_NONE)
878 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
879 return_code = ACCOUNT_ERROR_DB_FAILED;
885 ACCOUNT_INTERNAL_API int account_type_delete_by_app_id_offline(const char* app_id)
887 _INFO("account_type_delete_by_app_id starting");
889 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
891 guint pid = getpid();
893 _INFO("client Id = [%u]", pid);
895 int return_code = _account_db_open(1);
896 if (return_code != ACCOUNT_ERROR_NONE)
898 _ERR("_account_db_open() error, ret = %d", return_code);
906 _ERR("current daemon is not root user, uid=%d", uid);
910 _INFO("before _account_type_delete_by_app_id");
911 return_code = _account_type_delete_by_app_id(app_id);
912 _INFO("after _account_type_delete_by_app_id=[%d]", return_code);
914 if (return_code != ACCOUNT_ERROR_NONE)
916 _ERR("_account_type_delete_by_app_id error");
921 _INFO("account_type_delete_by_app_id_offline end");
923 if( g_hAccountDB == NULL )
926 return_code = _account_db_close();
927 if (return_code != ACCOUNT_ERROR_NONE)
929 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
930 return_code = ACCOUNT_ERROR_DB_FAILED;
936 static void _account_db_data_to_text(const char *textbuf, char **output)
938 if (textbuf && strlen(textbuf)>0) {
943 *output = strdup(textbuf);
946 static int _account_query_table_column_int(account_stmt pStmt, int pos)
949 ACCOUNT_ERROR("statement is null");
954 ACCOUNT_ERROR("invalid pos");
958 return sqlite3_column_int(pStmt, pos);
961 static const char *_account_query_table_column_text(account_stmt pStmt, int pos)
964 ACCOUNT_ERROR("statement is null");
969 ACCOUNT_ERROR("invalid pos");
973 return (const char*)sqlite3_column_text(pStmt, pos);
976 static void _account_convert_column_to_account(account_stmt hstmt, account_s *account_record)
978 const char *textbuf = NULL;
980 account_record->id = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_ID);
981 ACCOUNT_DEBUG("account_record->id =[%d]", account_record->id);
983 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_NAME);
984 _account_db_data_to_text(textbuf, &(account_record->user_name));
986 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_EMAIL_ADDRESS);
987 _account_db_data_to_text(textbuf, &(account_record->email_address));
989 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DISPLAY_NAME);
990 _account_db_data_to_text(textbuf, &(account_record->display_name));
992 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ICON_PATH);
993 _account_db_data_to_text(textbuf, &(account_record->icon_path));
995 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_SOURCE);
996 _account_db_data_to_text(textbuf, &(account_record->source));
998 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_PACKAGE_NAME);
999 _account_db_data_to_text(textbuf, &(account_record->package_name));
1001 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ACCESS_TOKEN);
1002 _account_db_data_to_text(textbuf, &(account_record->access_token));
1004 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DOMAIN_NAME);
1005 _account_db_data_to_text(textbuf, &(account_record->domain_name));
1007 account_record->auth_type = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_AUTH_TYPE);
1009 account_record->secret = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SECRET);
1011 account_record->sync_support = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SYNC_SUPPORT);
1013 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_0);
1014 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[0]));
1016 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_1);
1017 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[1]));
1019 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_2);
1020 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[2]));
1022 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_3);
1023 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[3]));
1025 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_4);
1026 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[4]));
1028 account_record->user_data_int[0] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_0);
1029 account_record->user_data_int[1] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_1);
1030 account_record->user_data_int[2] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_2);
1031 account_record->user_data_int[3] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_3);
1032 account_record->user_data_int[4] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_4);
1035 static void _account_convert_column_to_capability(account_stmt hstmt, account_capability_s *capability_record)
1037 const char *textbuf = NULL;
1039 _INFO("start _account_convert_column_to_capability()");
1040 capability_record->id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ID);
1042 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_KEY);
1043 _account_db_data_to_text(textbuf, &(capability_record->type));
1045 capability_record->value = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_VALUE);
1047 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_PACKAGE_NAME);
1048 _account_db_data_to_text(textbuf, &(capability_record->package_name));
1050 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_USER_NAME);
1051 _account_db_data_to_text(textbuf, &(capability_record->user_name));
1053 capability_record->account_id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ACCOUNT_ID);
1054 _INFO("type = %s, value = %d", capability_record->type, capability_record->value);
1055 _INFO("end _account_convert_column_to_capability()");
1058 static void _account_convert_column_to_custom(account_stmt hstmt, account_custom_s *custom_record)
1060 _INFO("start _account_convert_column_to_custom()");
1061 const char *textbuf = NULL;
1063 custom_record->account_id = _account_query_table_column_int(hstmt, ACCOUNT_CUSTOM_FIELD_ACCOUNT_ID);
1065 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_APP_ID);
1066 _account_db_data_to_text(textbuf, &(custom_record->app_id));
1068 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_KEY);
1069 _account_db_data_to_text(textbuf, &(custom_record->key));
1071 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_VALUE);
1072 _account_db_data_to_text(textbuf, &(custom_record->value));
1073 _INFO("key = %s, value = %s", custom_record->key, custom_record->value);
1074 _INFO("end _account_convert_column_to_custom()");
1077 static int _account_convert_account_to_sql(account_s *account, account_stmt hstmt, char *sql_value)
1083 /*Caution : Keep insert query orders.*/
1086 _account_query_bind_text(hstmt, count++, (char*)account->user_name);
1087 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], user_name=%s", account->id, account->user_name);
1089 /* 2. email address*/
1090 _account_query_bind_text(hstmt, count++, (char*)account->email_address);
1091 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], email_address=%s", account->id, account->email_address);
1093 /* 3. display name*/
1094 _account_query_bind_text(hstmt, count++, (char*)account->display_name);
1095 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], display_name=%s", account->id, account->display_name);
1098 _account_query_bind_text(hstmt, count++, (char*)account->icon_path);
1099 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], icon_path=%s", account->id, account->icon_path);
1102 _account_query_bind_text(hstmt, count++, (char*)account->source);
1103 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], source=%s", account->id, account->source);
1105 /* 6. package name*/
1106 _account_query_bind_text(hstmt, count++, (char*)account->package_name);
1107 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], package_name=%s", account->id, account->package_name);
1109 /* 7. access token*/
1110 _account_query_bind_text(hstmt, count++, (char*)account->access_token);
1111 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], access_token=%s", account->id, account->access_token);
1114 _account_query_bind_text(hstmt, count++, (char*)account->domain_name);
1115 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], domain_name=%s", account->id, account->domain_name);
1118 _account_query_bind_int(hstmt, count++, account->auth_type);
1119 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], auth_type=%d", account->id, account->auth_type);
1122 _account_query_bind_int(hstmt, count++, account->secret);
1123 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], secret=%d", account->id, account->secret);
1125 /* 11. sync_support */
1126 _account_query_bind_int(hstmt, count++, account->sync_support);
1127 _INFO("account_update_to_db_by_id_ex_p : after convert() : account_id[%d], sync_support=%d", account->id, account->sync_support);
1132 for(i=0; i< USER_TXT_CNT; i++)
1133 _account_query_bind_text(hstmt, count++, (char*)account->user_data_txt[i]);
1135 /* 13. user integer */
1136 for(i=0; i< USER_INT_CNT; i++)
1138 _account_query_bind_int(hstmt, count++, account->user_data_int[i]);
1139 _INFO("convert user_data_int : marshal_user_int data_int[%d]=%d", i, account->user_data_int[i]);
1147 static bool _account_get_capability_text_cb(const char* capability_type, account_capability_state_e capability_value, void *user_data)
1149 account_s *data = (account_s*)user_data;
1151 account_capability_s *cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
1153 if (cap_data == NULL)
1155 ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
1157 cap_data->type = _account_get_text(capability_type);
1158 cap_data->value = capability_value;
1159 _INFO("cap_data->type = %s, cap_data->value = %d", cap_data->type, cap_data->value);
1161 data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data);
1166 static bool _account_get_custom_text_cb(char* key, char* value, void *user_data)
1168 account_s *data = (account_s*)user_data;
1170 account_custom_s *custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
1172 if (custom_data == NULL) {
1173 ACCOUNT_DEBUG("_account_get_custom_text_cb :: malloc fail\n");
1176 ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
1178 custom_data->account_id = data->id;
1179 custom_data->app_id = _account_get_text(data->package_name);
1180 custom_data->key = _account_get_text(key);
1181 custom_data->value = _account_get_text(value);
1182 _INFO("custom_data->key = %s, custom_data->value = %s", custom_data->key, custom_data->value);
1184 data->custom_list = g_slist_append(data->custom_list, (gpointer)custom_data);
1189 static int _account_query_capability_by_account_id(capability_cb callback, int account_id, void *user_data )
1191 int error_code = ACCOUNT_ERROR_NONE;
1192 account_stmt hstmt = NULL;
1193 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1196 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1197 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
1198 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1200 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1202 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
1203 hstmt = _account_prepare_query(query);
1205 if( _account_db_err_code() == SQLITE_PERM ){
1206 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1207 return ACCOUNT_ERROR_PERMISSION_DENIED;
1210 rc = _account_query_step(hstmt);
1211 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1213 account_capability_s* capability_record = NULL;
1215 while (rc == SQLITE_ROW) {
1216 bool cb_ret = FALSE;
1217 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
1219 if (capability_record == NULL) {
1220 ACCOUNT_FATAL("malloc Failed");
1224 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
1226 _account_convert_column_to_capability(hstmt, capability_record);
1228 cb_ret = callback(capability_record->type, capability_record->value, user_data);
1230 _account_free_capability_with_items(capability_record);
1232 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
1234 rc = _account_query_step(hstmt);
1237 rc = _account_query_finalize(hstmt);
1238 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1241 error_code = ACCOUNT_ERROR_NONE;
1244 if (hstmt != NULL) {
1245 rc = _account_query_finalize(hstmt);
1246 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1250 pthread_mutex_unlock(&account_mutex);
1254 static int _account_query_custom_by_account_id(account_custom_cb callback, int account_id, void *user_data )
1256 int error_code = ACCOUNT_ERROR_NONE;
1257 account_stmt hstmt = NULL;
1258 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1261 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1262 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
1263 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1265 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1267 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
1268 hstmt = _account_prepare_query(query);
1270 if( _account_db_err_code() == SQLITE_PERM ){
1271 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1272 return ACCOUNT_ERROR_PERMISSION_DENIED;
1275 rc = _account_query_step(hstmt);
1277 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1279 account_custom_s* custom_record = NULL;
1281 while (rc == SQLITE_ROW) {
1282 bool cb_ret = FALSE;
1283 custom_record = (account_custom_s*) malloc(sizeof(account_custom_s));
1285 if (custom_record == NULL) {
1286 ACCOUNT_FATAL("malloc Failed");
1290 ACCOUNT_MEMSET(custom_record, 0x00, sizeof(account_custom_s));
1292 _account_convert_column_to_custom(hstmt, custom_record);
1294 cb_ret = callback(custom_record->key, custom_record->value, user_data);
1296 _account_free_custom_with_items(custom_record);
1298 ACCOUNT_CATCH_ERROR(cb_ret == TRUE, {}, ACCOUNT_ERROR_NONE, ("Callback func returs FALSE, its iteration is stopped!!!!\n"));
1300 rc = _account_query_step(hstmt);
1303 rc = _account_query_finalize(hstmt);
1304 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1307 error_code = ACCOUNT_ERROR_NONE;
1310 if (hstmt != NULL) {
1311 rc = _account_query_finalize(hstmt);
1312 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1316 pthread_mutex_unlock(&account_mutex);
1320 static int _account_compare_old_record(account_s *new_account, int account_id)
1322 int error_code = ACCOUNT_ERROR_NONE;
1323 account_stmt hstmt = NULL;
1324 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1326 account_s *old_account = NULL;
1328 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1329 ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1330 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1332 old_account = (account_s*)calloc(1, sizeof(account_s));
1333 if (old_account == NULL) {
1334 _ERR("Out of Memory");
1335 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1338 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1340 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
1341 hstmt = _account_prepare_query(query);
1343 rc = _account_query_step(hstmt);
1344 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1346 while (rc == SQLITE_ROW) {
1347 _account_convert_column_to_account(hstmt, old_account);
1348 rc = _account_query_step(hstmt);
1351 rc = _account_query_finalize(hstmt);
1352 ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1356 error_code = _account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account);
1357 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("account_query_capability_by_account_id error"));
1360 error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account);
1361 ACCOUNT_CATCH_ERROR((error_code == ACCOUNT_ERROR_NONE), {}, error_code, ("_account_query_custom_by_account_id error"));
1365 new_account->id = old_account->id;
1368 if(!new_account->user_name) {
1369 if(old_account->user_name)
1370 new_account->user_name = _account_get_text(old_account->user_name);
1374 if(!new_account->display_name) {
1375 if(old_account->display_name)
1376 new_account->display_name = _account_get_text(old_account->display_name);
1380 if(!new_account->email_address) {
1381 if(old_account->email_address)
1382 new_account->email_address = _account_get_text(old_account->email_address);
1386 if(!new_account->domain_name) {
1387 if(old_account->domain_name)
1388 new_account->domain_name = _account_get_text(old_account->domain_name);
1392 if(!new_account->icon_path) {
1393 if(old_account->icon_path)
1394 new_account->icon_path = _account_get_text(old_account->icon_path);
1398 if(!new_account->source) {
1399 if(old_account->source)
1400 new_account->source = _account_get_text(old_account->source);
1403 _ACCOUNT_FREE(new_account->package_name);
1404 new_account->package_name = _account_get_text(old_account->package_name);
1407 if(!new_account->access_token) {
1408 if(old_account->access_token)
1409 new_account->access_token = _account_get_text(old_account->access_token);
1414 for(i=0;i<USER_TXT_CNT;i++) {
1415 if(!new_account->user_data_txt[i]) {
1416 if(old_account->user_data_txt[i])
1417 new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]);
1422 if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) {
1423 new_account->auth_type = old_account->auth_type;
1427 if(new_account->secret== ACCOUNT_SECRECY_INVALID) {
1428 new_account->secret = old_account->secret;
1432 if(new_account->sync_support == ACCOUNT_SYNC_INVALID) {
1433 new_account->sync_support = old_account->sync_support;
1437 for(i=0;i<USER_INT_CNT;i++) {
1438 if(new_account->user_data_int[i] == 0) {
1439 new_account->user_data_int[i] = old_account->user_data_int[i];
1445 // user custom table
1449 _account_free_account_with_items(old_account);
1451 if (hstmt != NULL) {
1452 rc = _account_query_finalize(hstmt);
1453 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1457 return ACCOUNT_ERROR_NONE;
1460 GList* _account_query_account_by_package_name(const char* package_name, int *error_code)
1462 _INFO("_account_query_account_by_package_name start, package_name=[%s]", package_name);
1464 *error_code = ACCOUNT_ERROR_NONE;
1465 account_stmt hstmt = NULL;
1466 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1469 ACCOUNT_RETURN_VAL((package_name != NULL), {*error_code = ACCOUNT_ERROR_INVALID_PARAMETER;}, NULL, ("PACKAGE NAME IS NULL"));
1470 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {*error_code = ACCOUNT_ERROR_DB_NOT_OPENED;}, NULL, ("The database isn't connected."));
1472 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1474 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE package_name=?", ACCOUNT_TABLE);
1476 hstmt = _account_prepare_query(query);
1478 if( _account_db_err_code() == SQLITE_PERM ){
1479 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1480 *error_code = ACCOUNT_ERROR_PERMISSION_DENIED;
1484 int binding_count = 1;
1485 _account_query_bind_text(hstmt, binding_count++, package_name);
1487 rc = _account_query_step(hstmt);
1489 account_s* account_head = NULL;
1491 ACCOUNT_CATCH_ERROR_P(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.(%s)\n", package_name));
1495 account_head = (account_s*) malloc(sizeof(account_s));
1496 if (account_head == NULL) {
1497 ACCOUNT_FATAL("malloc Failed");
1498 if (hstmt != NULL) {
1499 rc = _account_query_finalize(hstmt);
1500 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {*error_code = rc;}, NULL, ("finalize error"));
1503 *error_code = ACCOUNT_ERROR_OUT_OF_MEMORY;
1506 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
1508 while (rc == SQLITE_ROW) {
1509 account_s* account_record = NULL;
1511 account_record = (account_s*) malloc(sizeof(account_s));
1513 if (account_record == NULL) {
1514 ACCOUNT_FATAL("malloc Failed");
1517 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
1519 _account_convert_column_to_account(hstmt, account_record);
1521 _INFO("Adding account_list");
1522 account_head->account_list = g_list_append(account_head->account_list, account_record);
1524 rc = _account_query_step(hstmt);
1528 rc = _account_query_finalize(hstmt);
1529 ACCOUNT_CATCH_ERROR_P((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1534 tmp = g_list_length(account_head->account_list);
1536 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
1537 account_s* testaccount = (account_s*)iter->data;
1539 _account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
1540 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
1543 *error_code = ACCOUNT_ERROR_NONE;
1548 rc = _account_query_finalize(hstmt);
1549 if (rc != ACCOUNT_ERROR_NONE) {
1551 _ERR("finalize error");
1556 pthread_mutex_unlock(&account_mutex);
1558 if( (*error_code != ACCOUNT_ERROR_NONE) && account_head ) {
1559 _account_glist_account_free(account_head->account_list);
1560 _ACCOUNT_FREE(account_head);
1561 account_head = NULL;
1564 if ((*error_code == ACCOUNT_ERROR_NONE) && account_head != NULL)
1566 _INFO("Returning account_list");
1567 // _remove_sensitive_info_from_non_owning_account_list(getpid(), account_head->account_list);
1568 GList* result = account_head->account_list;
1569 _ACCOUNT_FREE(account_head);
1575 ACCOUNT_INTERNAL_API int account_query_account_by_package_name_offline(account_cb callback, const char *package_name, void *user_data)
1577 _INFO("account_query_from_db_by_package_name_offline");
1579 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
1580 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("callback is null!"));
1582 int return_code = -1;
1583 GList* account_list = NULL;
1586 return_code = _account_db_open(0);
1587 if (return_code != ACCOUNT_ERROR_NONE)
1589 _ERR("_account_db_open() error, ret = %d", return_code);
1597 _ERR("current process user is not root, uid=%d", uid);
1598 return_code = ACCOUNT_ERROR_PERMISSION_DENIED;
1602 _INFO("before _account_query_from_db_by_package_name");
1603 account_list = _account_query_account_by_package_name(package_name, &return_code);
1604 _INFO("after _account_query_from_db_by_package_name=[%d]", return_code);
1606 if (return_code != ACCOUNT_ERROR_NONE)
1608 _ERR("_account_query_from_db_by_package_name error");
1612 for (iter = account_list; iter != NULL; iter = g_list_next(iter))
1614 _INFO("iterating received account_list");
1615 account_s *account = NULL;
1616 account = (account_s*)iter->data;
1618 if (callback((account_h)account, user_data) == false)
1620 _INFO("application callback requested to discontinue.");
1626 _account_glist_account_free(account_list);
1627 if( g_hAccountDB == NULL )
1630 return_code = _account_db_close();
1631 if (return_code != ACCOUNT_ERROR_NONE)
1633 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
1634 return_code = ACCOUNT_ERROR_DB_FAILED;
1637 _INFO("account_query_account_by_package_name_offline end");
1641 static void _account_insert_delete_update_notification_send(char *noti_name, int pid)
1644 vsm_zone_h effective_zone, real_zone;
1647 _ERR("Noti Name is NULL!!!!!!\n");
1652 ctx = vsm_create_context();
1655 _ERR( "Failed to initialize domain control vsm context.");
1659 effective_zone = vsm_lookup_zone_by_pid(ctx, pid);
1660 if(effective_zone == NULL) {
1661 _ERR( "Failed vsm_lookup_zone_by_pid.");
1665 _INFO( "before set vsm_join_zone()");
1666 real_zone = vsm_join_zone(effective_zone);
1667 _INFO( "after set vsm_join_zone()");
1670 _INFO("noti_type = %s", noti_name);
1672 if (vconf_set_str(VCONFKEY_ACCOUNT_MSG_STR, noti_name) != 0) {
1673 _ERR("Vconf MSG Str set FAILED !!!!!!\n");;
1676 _INFO( "before recover vsm_join_zone()");
1677 vsm_join_zone(real_zone);
1678 _INFO( "after recover vsm_join_zone()");
1680 _INFO( "before vsm_cleanup_context()");
1681 vsm_cleanup_context(ctx);
1682 _INFO( "after vsm_cleanup_context()");
1685 int _account_delete_from_db_by_package_name_offline(const char *package_name)
1687 int error_code = ACCOUNT_ERROR_NONE;
1688 account_stmt hstmt = NULL;
1689 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1691 int ret_transaction = 0;
1692 bool is_success = FALSE;
1693 int binding_count = 1;
1694 GSList *account_id_list = NULL;
1697 // It only needs list of ids, does not need to query sensitive info. So sending 0
1698 GList* account_list_temp = _account_query_account_by_package_name(package_name, &ret);
1699 if (account_list_temp == NULL)
1701 _ERR("_account_query_account_by_package_name returned NULL");
1702 return ACCOUNT_ERROR_DB_FAILED;
1705 if( _account_db_err_code() == SQLITE_PERM ){
1706 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1707 _account_glist_account_free(account_list_temp);
1708 return ACCOUNT_ERROR_PERMISSION_DENIED;
1711 if(ret != ACCOUNT_ERROR_NONE){
1712 _account_glist_account_free(account_list_temp);
1716 account_list_temp = g_list_first(account_list_temp);
1717 _INFO("account_list_temp length=[%d]",g_list_length(account_list_temp));
1720 for (iter = account_list_temp; iter != NULL; iter = g_list_next(iter))
1722 _INFO("iterating account_list_temp");
1723 account_s *account = NULL;
1724 _INFO("Before iter->data");
1725 account = (account_s*)iter->data;
1726 _INFO("After iter->data");
1727 if (account != NULL)
1729 char id[256] = {0, };
1731 ACCOUNT_MEMSET(id, 0, 256);
1733 ACCOUNT_SNPRINTF(id, 256, "%d", account->id);
1735 _INFO("Adding account id [%s]", id);
1736 account_id_list = g_slist_append(account_id_list, g_strdup(id));
1740 _account_glist_account_free(account_list_temp);
1741 /* transaction control required*/
1742 ret_transaction = _account_begin_transaction();
1744 if( _account_db_err_code() == SQLITE_PERM ){
1745 pthread_mutex_unlock(&account_mutex);
1746 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1747 return ACCOUNT_ERROR_PERMISSION_DENIED;
1750 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
1751 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
1752 pthread_mutex_unlock(&account_mutex);
1753 return ACCOUNT_ERROR_DATABASE_BUSY;
1754 }else if (ret_transaction != ACCOUNT_ERROR_NONE) {
1755 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
1756 pthread_mutex_unlock(&account_mutex);
1757 return ret_transaction;
1760 /* delete custom table */
1761 ACCOUNT_MEMSET(query, 0, sizeof(query));
1762 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", ACCOUNT_CUSTOM_TABLE);
1764 hstmt = _account_prepare_query(query);
1766 if( _account_db_err_code() == SQLITE_PERM ){
1767 _account_end_transaction(FALSE);
1768 pthread_mutex_unlock(&account_mutex);
1769 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
1770 return ACCOUNT_ERROR_PERMISSION_DENIED;
1773 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
1774 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
1777 _account_query_bind_text(hstmt, binding_count++, package_name);
1779 rc = _account_query_step(hstmt);
1780 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1782 rc = _account_query_finalize(hstmt);
1783 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1786 /* delete capability table */
1787 ACCOUNT_MEMSET(query, 0, sizeof(query));
1788 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", CAPABILITY_TABLE);
1790 hstmt = _account_prepare_query(query);
1792 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
1793 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
1796 _account_query_bind_text(hstmt, binding_count++, package_name);
1798 rc = _account_query_step(hstmt);
1799 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1801 rc = _account_query_finalize(hstmt);
1802 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1805 /* delete account table */
1806 ACCOUNT_MEMSET(query, 0, sizeof(query));
1808 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", ACCOUNT_TABLE);
1810 hstmt = _account_prepare_query(query);
1811 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
1812 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
1815 _account_query_bind_text(hstmt, binding_count++, package_name);
1817 rc = _account_query_step(hstmt);
1818 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));
1820 rc = _account_query_finalize(hstmt);
1821 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1827 if (hstmt != NULL) {
1828 rc = _account_query_finalize(hstmt);
1829 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1833 ret_transaction = _account_end_transaction(is_success);
1835 if (ret_transaction != ACCOUNT_ERROR_NONE) {
1836 ACCOUNT_ERROR("account_delete:_account_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
1838 if (is_success == true) {
1839 GSList* gs_iter = NULL;
1840 for (gs_iter = account_id_list; gs_iter != NULL; gs_iter = g_slist_next(gs_iter)) {
1841 char* p_tmpid = NULL;
1842 p_tmpid = (char*)gs_iter->data;
1844 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%s", ACCOUNT_NOTI_NAME_DELETE, p_tmpid);
1845 ACCOUNT_SLOGD("%s", buf);
1846 _account_insert_delete_update_notification_send(buf, getpid());
1847 _ACCOUNT_FREE(p_tmpid);
1849 g_slist_free(account_id_list);
1853 pthread_mutex_unlock(&account_mutex);
1855 _INFO("_account_delete_from_db_by_package_name_offline end");
1859 ACCOUNT_INTERNAL_API int account_delete_from_db_by_package_name_offline(const char *package_name)
1861 _INFO("_account_delete_from_db_by_package_name_offline");
1863 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
1865 int return_code = _account_db_open(1);
1866 if (return_code != ACCOUNT_ERROR_NONE)
1868 _ERR("_account_db_open() error, ret = %d", return_code);
1876 _ERR("current process user is not root, uid=%d", uid);
1877 return_code = ACCOUNT_ERROR_PERMISSION_DENIED;
1881 _INFO("before _account_delete_from_db_by_package_name_offline");
1882 return_code = _account_delete_from_db_by_package_name_offline(package_name);
1883 _INFO("after _account_delete_from_db_by_package_name_offline=[%d]", return_code);
1885 if (return_code != ACCOUNT_ERROR_NONE)
1887 _ERR("_account_delete_from_db_by_package_name_offline error");
1892 _INFO("account_delete_from_db_by_package_name_offline end");
1894 if( g_hAccountDB == NULL )
1897 return_code = _account_db_close();
1898 if (return_code != ACCOUNT_ERROR_NONE)
1900 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
1901 return_code = ACCOUNT_ERROR_DB_FAILED;
1907 static int _account_get_package_name_from_account_id(int account_id, char **package_name)
1909 int error_code = ACCOUNT_ERROR_NONE;
1910 account_stmt hstmt = NULL;
1911 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1913 account_s *old_account = NULL;
1915 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1916 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1918 old_account = (account_s*)calloc(1, sizeof(account_s));
1919 if (old_account == NULL) {
1920 _ERR("Out Of memory");
1921 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1924 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1926 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
1927 hstmt = _account_prepare_query(query);
1929 rc = _account_query_step(hstmt);
1930 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1932 while (rc == SQLITE_ROW) {
1933 _account_convert_column_to_account(hstmt, old_account);
1934 rc = _account_query_step(hstmt);
1937 rc = _account_query_finalize(hstmt);
1938 ACCOUNT_CATCH_ERROR((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1941 // get package name.
1942 *package_name = _account_get_text(old_account->package_name);
1947 _account_free_account_with_items(old_account);
1949 if (hstmt != NULL) {
1950 rc = _account_query_finalize(hstmt);
1951 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1959 static int _account_update_capability(account_s *account, int account_id)
1962 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1963 account_stmt hstmt = NULL;
1965 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1967 if (g_slist_length( account->capablity_list)==0) {
1968 ACCOUNT_ERROR( "_account_update_capability, no capability\n");
1969 return ACCOUNT_ERROR_NONE;
1972 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
1974 rc = _account_get_record_count(query);
1977 ACCOUNT_SLOGI( "_account_update_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
1978 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1981 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1983 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id=? ", CAPABILITY_TABLE);
1984 hstmt = _account_prepare_query(query);
1986 _account_query_bind_int(hstmt, count++, (int)account_id);
1987 rc = _account_query_step(hstmt);
1989 if (rc != SQLITE_DONE) {
1990 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1991 return ACCOUNT_ERROR_DB_FAILED;
1993 rc = _account_query_finalize(hstmt);
1994 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
1999 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
2002 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2003 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
2004 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
2006 hstmt = _account_prepare_query(query);
2008 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
2010 account_capability_s* cap_data = NULL;
2011 cap_data = (account_capability_s*)iter->data;
2013 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
2014 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
2015 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
2016 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
2017 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
2018 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
2019 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
2020 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
2021 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
2022 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
2024 rc = _account_query_step(hstmt);
2026 if (rc != SQLITE_DONE) {
2027 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2031 rc = _account_query_finalize(hstmt);
2032 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2037 return ACCOUNT_ERROR_NONE;
2040 static int _account_update_custom(account_s *account, int account_id)
2043 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2044 account_stmt hstmt = NULL;
2046 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
2048 if (g_slist_length( account->custom_list)==0) {
2049 ACCOUNT_DEBUG( "_account_update_custom, no custom data\n");
2050 return ACCOUNT_ERROR_NONE;
2053 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
2055 rc = _account_get_record_count(query);
2057 if( _account_db_err_code() == SQLITE_PERM ){
2058 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2059 pthread_mutex_unlock(&account_mutex);
2060 return ACCOUNT_ERROR_PERMISSION_DENIED;
2061 } else if( _account_db_err_code() == SQLITE_BUSY ){
2062 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
2063 pthread_mutex_unlock(&account_mutex);
2064 return ACCOUNT_ERROR_DATABASE_BUSY;
2068 ACCOUNT_SLOGE( "_account_update_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
2069 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2072 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2074 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId=? ", ACCOUNT_CUSTOM_TABLE);
2075 hstmt = _account_prepare_query(query);
2077 _account_query_bind_int(hstmt, count++, (int)account_id);
2078 rc = _account_query_step(hstmt);
2080 if (rc == SQLITE_BUSY) {
2081 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2082 return ACCOUNT_ERROR_DATABASE_BUSY;
2083 } else if (rc != SQLITE_DONE) {
2084 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2085 return ACCOUNT_ERROR_DB_FAILED;
2088 rc = _account_query_finalize(hstmt);
2089 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2094 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
2097 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2098 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AccountId, AppId, Key, Value) VALUES "
2099 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
2101 hstmt = _account_prepare_query(query);
2103 if( _account_db_err_code() == SQLITE_PERM ){
2104 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2105 return ACCOUNT_ERROR_PERMISSION_DENIED;
2108 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
2110 account_custom_s* custom_data = NULL;
2111 custom_data = (account_custom_s*)iter->data;
2113 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
2114 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
2115 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
2116 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
2117 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
2118 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
2119 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
2120 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
2122 rc = _account_query_step(hstmt);
2124 if (rc != SQLITE_DONE) {
2125 ACCOUNT_ERROR( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2129 rc = _account_query_finalize(hstmt);
2130 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2135 return ACCOUNT_ERROR_NONE;
2138 int _account_update_account(account_s *account, int account_id)
2140 int rc = 0, binding_count =0;
2141 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2142 int error_code = ACCOUNT_ERROR_NONE, count=0, ret_transaction = 0;
2143 account_stmt hstmt = NULL;
2145 if (!account->package_name) {
2146 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
2147 return ACCOUNT_ERROR_INVALID_PARAMETER;
2150 /* Check account_id*/
2151 char *package_name = NULL;
2153 error_code = _account_get_package_name_from_account_id(account_id, &package_name);
2155 if(error_code != ACCOUNT_ERROR_NONE || package_name == NULL){
2156 ACCOUNT_ERROR("No package name with account_id\n");
2157 _ACCOUNT_FREE(package_name);
2158 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2161 _ACCOUNT_FREE(package_name);
2163 if (error_code != ACCOUNT_ERROR_NONE) {
2164 ACCOUNT_ERROR("No permission to update\n");
2165 return ACCOUNT_ERROR_PERMISSION_DENIED;
2168 error_code = _account_compare_old_record(account, account_id);
2169 if (error_code != ACCOUNT_ERROR_NONE) {
2170 ACCOUNT_ERROR("_account_compare_old_record fail\n");
2174 if (_account_db_err_code() == SQLITE_PERM ) {
2175 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2176 return ACCOUNT_ERROR_PERMISSION_DENIED;
2177 } else if( _account_db_err_code() == SQLITE_BUSY ) {
2178 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
2179 return ACCOUNT_ERROR_DATABASE_BUSY;
2182 if (!account->user_name && !account->display_name && !account->email_address) {
2183 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
2184 return ACCOUNT_ERROR_INVALID_PARAMETER;
2187 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2189 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
2191 count = _account_get_record_count(query);
2193 ACCOUNT_DEBUG(" Account record not found, count = %d\n", count);
2194 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2197 /* transaction control required*/
2198 ret_transaction = _account_begin_transaction();
2199 if( ret_transaction == ACCOUNT_ERROR_DATABASE_BUSY ){
2200 ACCOUNT_ERROR( "database busy(%s)", _account_db_err_msg());
2201 pthread_mutex_unlock(&account_mutex);
2202 return ACCOUNT_ERROR_DATABASE_BUSY;
2205 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2206 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
2207 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
2208 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
2209 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
2211 hstmt = _account_prepare_query(query);
2213 if( _account_db_err_code() == SQLITE_PERM ){
2214 ret_transaction = _account_end_transaction(FALSE);
2215 ACCOUNT_ERROR( "Access failed(%s)", _account_db_err_msg());
2216 return ACCOUNT_ERROR_PERMISSION_DENIED;
2219 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)));
2221 binding_count = _account_convert_account_to_sql(account, hstmt, query);
2222 _account_query_bind_int(hstmt, binding_count++, account_id);
2224 rc = _account_query_step(hstmt);
2225 if (rc != SQLITE_DONE) {
2226 ACCOUNT_SLOGE( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2229 rc = _account_query_finalize(hstmt);
2230 ACCOUNT_RETURN_VAL((rc == ACCOUNT_ERROR_NONE), {}, rc, ("finalize error"));
2233 _INFO("update query=%s", query);
2235 /*update capability*/
2236 error_code = _account_update_capability(account, account_id);
2237 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2238 ret_transaction = _account_end_transaction(FALSE);
2239 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2244 error_code = _account_update_custom(account, account_id);
2245 if(error_code != ACCOUNT_ERROR_NONE && error_code!= ACCOUNT_ERROR_RECORD_NOT_FOUND){
2246 ret_transaction = _account_end_transaction(FALSE);
2247 ACCOUNT_ERROR("update capability Failed, trying to roll back(%x) !!!\n", ret_transaction);
2251 ret_transaction = _account_end_transaction(TRUE);
2253 _INFO("update end");
2257 int _account_update_to_db_by_id_offline(account_s* account, int account_id)
2259 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
2260 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
2261 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2262 int error_code = ACCOUNT_ERROR_NONE;
2263 account_s* data = (account_s*)account;
2265 pthread_mutex_lock(&account_mutex);
2267 error_code = _account_update_account(data, account_id);
2269 if(error_code != ACCOUNT_ERROR_NONE) {
2270 pthread_mutex_unlock(&account_mutex);
2274 pthread_mutex_unlock(&account_mutex);
2277 ACCOUNT_SNPRINTF(buf, sizeof(buf), "%s:%d", ACCOUNT_NOTI_NAME_UPDATE, account_id);
2278 _account_insert_delete_update_notification_send(buf, getpid());
2280 return ACCOUNT_ERROR_NONE;
2283 ACCOUNT_INTERNAL_API int account_update_to_db_by_id_offline(account_h account, int account_id)
2285 _INFO("account_update_to_db_by_id_offline");
2287 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account_h is null!"));
2288 ACCOUNT_RETURN_VAL((account_id >= 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account_id is invalid data!"));
2290 int return_code = _account_db_open(1);
2291 if (return_code != ACCOUNT_ERROR_NONE)
2293 _ERR("_account_db_open() error, ret = %d", return_code);
2301 _ERR("current process user is not root, uid=%d", uid);
2302 return_code = ACCOUNT_ERROR_PERMISSION_DENIED;
2306 _INFO("before _account_update_to_db_by_id_offline");
2307 return_code = _account_update_to_db_by_id_offline((account_s *)account, account_id);
2308 _INFO("after _account_update_to_db_by_id_offline=[%d]", return_code);
2310 if (return_code != ACCOUNT_ERROR_NONE)
2312 _ERR("_account_update_to_db_by_id_offline error");
2317 _INFO("account_update_to_db_by_id_offline end");
2319 if( g_hAccountDB == NULL )
2322 return_code = _account_db_close();
2323 if (return_code != ACCOUNT_ERROR_NONE)
2325 ACCOUNT_DEBUG("_account_db_close() fail[%d]", return_code);
2326 return_code = ACCOUNT_ERROR_DB_FAILED;