4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Wonyoung Lee <wy1115.lee@samsung.com>, Sungchan Kim <sungchan81.kim@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
30 #include <account-private.h>
34 static sqlite3* g_hAccountDB = NULL;
35 static int g_refCntDB = 0;
36 pthread_mutex_t account_mutex = PTHREAD_MUTEX_INITIALIZER;
38 static char *_account_get_text(const char *text_data);
39 static int _account_gslist_free(GSList* list);
40 static int _account_glist_free(GList* list);
41 static char *_account_query_table_column_text(account_stmt pStmt, int pos);
42 static int _account_insert_custom(account_s *account, int account_id);
43 static int _account_update_custom(account_s *account, int account_id);
44 static int _account_query_custom_by_account_id(account_custom_cb cb_func, int account_id, void *user_data );
46 static const char *_account_db_err_msg()
48 assert(NULL != g_hAccountDB);
49 return sqlite3_errmsg(g_hAccountDB);
52 static void _account_insert_delete_update_notification_send(char *noti_name)
55 ACCOUNT_ERROR("Noti Name is NULL!!!!!!\n");
59 ACCOUNT_DEBUG("Sending notification with value %s\n", noti_name);
61 if (vconf_set_str(VCONFKEY_ACCOUNT_MSG_STR, noti_name) == 0) {
62 ACCOUNT_VERBOSE("Vconf MSG Str set SUCCESS !!!!!!\n");;
64 ACCOUNT_ERROR("Vconf MSG Str set FAILED !!!!!!\n");;
68 static int _account_get_record_count(char* query)
72 account_stmt pStmt = NULL;
74 assert(NULL != query);
75 assert(NULL != g_hAccountDB);
76 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
78 rc = sqlite3_step(pStmt);
79 if (SQLITE_ROW != rc) {
80 ACCOUNT_ERROR("sqlite3_step() failed(%d, %s).", rc, _account_db_err_msg());
81 sqlite3_finalize(pStmt);
82 return ACCOUNT_ERROR_DB_FAILED;
85 ncount = sqlite3_column_int(pStmt, 0);
87 ACCOUNT_VERBOSE("count : %d, End", ncount);
88 sqlite3_finalize(pStmt);
93 static int _account_execute_query(char *query)
96 char* pszErrorMsg = NULL;
98 assert(NULL != query);
99 assert(NULL != g_hAccountDB);
101 ACCOUNT_INFO("query : %s", query);
103 rc = sqlite3_exec(g_hAccountDB, query, NULL, NULL, &pszErrorMsg);
104 if (SQLITE_OK != rc) {
105 ACCOUNT_FATAL("sqlite3_exec(%s) failed(%s).", query, pszErrorMsg);
106 sqlite3_free(pszErrorMsg);
112 static int _account_begin_transaction(void)
116 ret = _account_execute_query("BEGIN IMMEDIATE TRANSACTION");
118 if (ret != SQLITE_OK) {
119 ACCOUNT_FATAL("_account_svc_begin_transaction fail :: %d", ret);
120 return ACCOUNT_ERROR_DB_FAILED;
123 return ACCOUNT_ERROR_NONE;
126 static int _account_end_transaction(bool is_success)
130 if (is_success == true) {
131 ret = _account_execute_query("COMMIT TRANSACTION");
133 ret = _account_execute_query("ROLLBACK TRANSACTION");
136 if (ret != SQLITE_OK) {
137 ACCOUNT_FATAL("_account_svc_end_transaction fail :: %d", ret);
138 return ACCOUNT_ERROR_DB_FAILED;
141 return ACCOUNT_ERROR_NONE;
144 static int _account_create_all_tables(void)
147 int error_code = ACCOUNT_ERROR_NONE;
148 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
151 ACCOUNT_MEMSET(query, 0, sizeof(query));
153 ACCOUNT_VERBOSE("_account_create_all_tables begin");
155 /*Create the account table*/
156 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_TABLE);
157 rc = _account_get_record_count(query);
159 ACCOUNT_MEMSET(query, 0, sizeof(query));
160 ACCOUNT_SNPRINTF(query, sizeof(query), ACCOUNT_SCHEMA, ACCOUNT_TABLE);
161 ACCOUNT_INFO("Create %s table", ACCOUNT_TABLE);
162 rc = _account_execute_query(query);
163 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
166 /*Create capability table*/
167 ACCOUNT_MEMSET(query, 0, sizeof(query));
168 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", CAPABILITY_TABLE);
169 rc = _account_get_record_count(query);
171 ACCOUNT_MEMSET(query, 0, sizeof(query));
172 ACCOUNT_SNPRINTF(query, sizeof(query), CAPABILITY_SCHEMA, CAPABILITY_TABLE);
173 ACCOUNT_INFO("Create %s table", CAPABILITY_TABLE);
174 rc = _account_execute_query(query);
175 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
178 /* Create account custom table */
179 ACCOUNT_MEMSET(query, 0, sizeof(query));
180 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_CUSTOM_TABLE);
181 rc = _account_get_record_count(query);
183 ACCOUNT_MEMSET(query, 0, sizeof(query));
184 ACCOUNT_SNPRINTF(query, sizeof(query), ACCOUNT_CUSTOM_SCHEMA, ACCOUNT_CUSTOM_TABLE);
185 ACCOUNT_INFO("Create %s table", ACCOUNT_CUSTOM_TABLE);
186 rc = _account_execute_query(query);
187 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
190 /* Create account type table */
191 ACCOUNT_MEMSET(query, 0, sizeof(query));
192 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", ACCOUNT_TYPE_TABLE);
193 rc = _account_get_record_count(query);
195 ACCOUNT_MEMSET(query, 0, sizeof(query));
196 ACCOUNT_SNPRINTF(query, sizeof(query), ACCOUNT_TYPE_SCHEMA, ACCOUNT_TYPE_TABLE);
197 ACCOUNT_INFO("Create %s table", ACCOUNT_TYPE_TABLE);
198 rc = _account_execute_query(query);
199 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
202 /* Create label table */
203 ACCOUNT_MEMSET(query, 0, sizeof(query));
204 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s')", LABEL_TABLE);
205 rc = _account_get_record_count(query);
207 ACCOUNT_MEMSET(query, 0, sizeof(query));
208 ACCOUNT_SNPRINTF(query, sizeof(query), LABEL_SCHEMA, LABEL_TABLE);
209 ACCOUNT_INFO("Create %s table", LABEL_TABLE);
210 rc = _account_execute_query(query);
211 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_execute_query(%s) failed(%d, %s).\n", query, rc, _account_db_err_msg()));
217 static bool _account_check_is_all_table_exists()
220 char query[ACCOUNT_SQL_LEN_MAX] = {0,};
221 ACCOUNT_MEMSET(query, 0, sizeof(query));
223 ACCOUNT_VERBOSE("_account_check_is_all_table_exists");
225 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from sqlite_master where name in ('%s', '%s')",
226 ACCOUNT_TABLE, CAPABILITY_TABLE);
227 rc = _account_get_record_count(query);
228 if (rc != ACCOUNT_TABLE_TOTAL_COUNT) {
229 ACCOUNT_INFO("Table count is not matched rc=%d\n", rc);
233 ACCOUNT_VERBOSE("END of _account_check_is_all_table_exists\n");
238 static int _account_db_open(void)
241 bool is_success = false;
242 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
244 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
247 rc = db_util_open(ACCOUNT_DB_NAME, &g_hAccountDB, DB_UTIL_REGISTER_HOOK_METHOD);
248 ACCOUNT_RETURN_VAL((rc == SQLITE_OK), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected. rc : %d", rc));
252 ACCOUNT_INFO("_account_db_open: The database connected. refcnt=%d ", g_refCntDB);
256 ACCOUNT_INFO("The database already connected. refcnt=%d ", g_refCntDB);
259 return ACCOUNT_ERROR_NONE;
262 static int _account_db_close(void)
268 if (g_refCntDB > 0) {
270 ACCOUNT_INFO("_account_svc_db_close: The database disconnected. refcnt=%d ", g_refCntDB);
272 if (g_refCntDB == 0) {
273 rc = db_util_close(g_hAccountDB);
274 ACCOUNT_RETURN_VAL((rc == SQLITE_OK), {}, ACCOUNT_ERROR_DB_FAILED, ("The database isn't connected. rc : %d", rc));
276 ACCOUNT_INFO( "_account_svc_db_close: The database disconnected really. ");
278 ret = ACCOUNT_ERROR_NONE;
280 ACCOUNT_ERROR( "_account_svc_db_close: No handle(). refcnt=%d ", g_refCntDB);
281 ret = ACCOUNT_ERROR_DB_FAILED;
287 static int _account_connect(void)
289 int error_code = ACCOUNT_ERROR_NONE;
291 pthread_mutex_lock(&account_mutex);
293 ACCOUNT_VERBOSE("db path = %s\n", ACCOUNT_DB_NAME);
295 error_code = _account_db_open();
296 if (ACCOUNT_ERROR_NONE != error_code) {
297 ACCOUNT_ERROR("The database isn't connected.\n");
298 pthread_mutex_unlock(&account_mutex);
299 return ACCOUNT_ERROR_DB_NOT_OPENED;
302 if (FALSE == _account_check_is_all_table_exists())
303 error_code = _account_create_all_tables();
305 pthread_mutex_unlock(&account_mutex);
306 return ACCOUNT_ERROR_NONE;
309 int account_connect (void)
311 return _account_connect();
314 static int _account_disconnect(void)
316 int error_code = ACCOUNT_ERROR_NONE;
318 pthread_mutex_lock(&account_mutex);
319 ACCOUNT_INFO("db path = %s have been closed!!!\n", ACCOUNT_DB_NAME);
321 error_code = _account_db_close();
322 pthread_mutex_unlock(&account_mutex);
327 int account_disconnect (void)
329 return _account_disconnect();
332 static int _account_free_capability_items(account_capability_s *data)
334 _ACCOUNT_FREE(data->type);
335 _ACCOUNT_FREE(data->package_name);
336 _ACCOUNT_FREE(data->user_name);
338 return ACCOUNT_ERROR_NONE;
341 static int _account_custom_item_free(account_custom_s *data)
343 _ACCOUNT_FREE(data->app_id);
344 _ACCOUNT_FREE(data->key);
345 _ACCOUNT_FREE(data->value);
347 return ACCOUNT_ERROR_NONE;
350 static int _account_custom_gslist_free(GSList* list)
352 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL"));
356 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
357 account_custom_s *custom_data = (account_custom_s*)iter->data;
358 _account_custom_item_free(custom_data);
359 _ACCOUNT_FREE(custom_data);
365 return ACCOUNT_ERROR_NONE;
368 static int _account_free_account_items(account_s *data)
370 _ACCOUNT_FREE(data->user_name);
371 _ACCOUNT_FREE(data->email_address);
372 _ACCOUNT_FREE(data->display_name);
373 _ACCOUNT_FREE(data->icon_path);
374 _ACCOUNT_FREE(data->source);
375 _ACCOUNT_FREE(data->package_name);
376 _ACCOUNT_FREE(data->domain_name);
379 for(i=0;i<USER_TXT_CNT;i++)
380 _ACCOUNT_FREE(data->user_data_txt[i]);
382 _account_gslist_free(data->capablity_list);
383 _account_glist_free(data->account_list);
384 _account_custom_gslist_free(data->custom_list);
386 return ACCOUNT_ERROR_NONE;
389 static int _account_gslist_free(GSList* list)
391 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL"));
395 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
396 account_capability_s *cap_data = (account_capability_s*)iter->data;
397 _account_free_capability_items(cap_data);
398 _ACCOUNT_FREE(cap_data);
404 return ACCOUNT_ERROR_NONE;
407 static int _account_glist_free(GList* list)
409 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Glist is NULL"));
413 for (iter = list; iter != NULL; iter = g_list_next(iter)) {
414 account_s *account_record = (account_s*)iter->data;
415 _account_free_account_items(account_record);
416 _ACCOUNT_FREE(account_record);
422 return ACCOUNT_ERROR_NONE;
425 static gboolean _account_check_duplicated(account_s *data)
427 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
430 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
432 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s where user_name='%s' and domain_name='%s'"
433 , ACCOUNT_TABLE, data->user_name, data->domain_name);
435 count = _account_get_record_count(query);
437 ACCOUNT_INFO("_account_check_duplicated : duplicated %d account(s) exist!, user_name=%s, domain_name=%s\n",
438 count, data->user_name, data->domain_name );
445 static int _account_get_next_sequence(char *pszName)
448 account_stmt pStmt = NULL;
450 char szQuery[ACCOUNT_SQL_LEN_MAX] = {0,};
452 ACCOUNT_VERBOSE( "[Enter] pszName:%s\n", pszName);
454 ACCOUNT_MEMSET(szQuery, 0x00, sizeof(szQuery));
455 ACCOUNT_SNPRINTF(szQuery, sizeof(szQuery), "SELECT max(seq) FROM %s where name = '%s' ", ACCOUNT_SQLITE_SEQ, pszName);
456 rc = sqlite3_prepare_v2(g_hAccountDB, szQuery, strlen(szQuery), &pStmt, NULL);
458 rc = sqlite3_step(pStmt);
459 max_seq = sqlite3_column_int(pStmt, 0);
462 ACCOUNT_VERBOSE( "sqlite3_column_int, rc=%d, max_seq=%d\n", rc, max_seq);
464 /*Finalize Statement*/
465 rc = sqlite3_finalize(pStmt);
471 static account_stmt _account_prepare_query(char *query)
474 account_stmt pStmt = NULL;
476 ACCOUNT_RETURN_VAL((query != NULL), {}, NULL, ("query is NULL"));
478 ACCOUNT_INFO( "prepare query : %s", query);
480 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
481 ACCOUNT_RETURN_VAL((SQLITE_OK == rc), {}, NULL, ("sqlite3_prepare_v2(%s) failed(%s).", query, _account_db_err_msg()));
486 static int _account_query_bind_int(account_stmt pStmt, int pos, int num)
488 assert(NULL != pStmt);
490 return sqlite3_bind_int(pStmt, pos, num);
493 static int _account_query_bind_text(account_stmt pStmt, int pos, const char *str)
495 assert(NULL != pStmt);
498 return sqlite3_bind_text(pStmt, pos, (const char*)str, strlen(str), SQLITE_STATIC);
500 return sqlite3_bind_null(pStmt, pos);
503 static int _account_convert_account_to_sql(account_s *account, account_stmt hstmt, char *sql_value)
505 ACCOUNT_VERBOSE( "_account_convert_account_to_sql");
508 /*Caution : Keep insert query orders.*/
511 _account_query_bind_text(hstmt, count++, (char*)account->user_name);
513 /* 2. email address*/
514 _account_query_bind_text(hstmt, count++, (char*)account->email_address);
517 _account_query_bind_text(hstmt, count++, (char*)account->display_name);
520 _account_query_bind_text(hstmt, count++, (char*)account->icon_path);
523 _account_query_bind_text(hstmt, count++, (char*)account->source);
526 _account_query_bind_text(hstmt, count++, (char*)account->package_name);
529 _account_query_bind_text(hstmt, count++, (char*)account->access_token);
532 _account_query_bind_text(hstmt, count++, (char*)account->domain_name);
535 _account_query_bind_int(hstmt, count++, account->auth_type);
538 _account_query_bind_int(hstmt, count++, account->secret);
540 /* 11. sync_support */
541 _account_query_bind_int(hstmt, count++, account->sync_support);
546 for(i=0; i< USER_TXT_CNT; i++)
547 _account_query_bind_text(hstmt, count++, (char*)account->user_data_txt[i]);
549 /* 13. user integer */
550 for(i=0; i< USER_INT_CNT; i++)
551 _account_query_bind_int(hstmt, count++, account->user_data_int[i]);
556 static void _account_query_finalize(account_stmt pStmt)
561 ACCOUNT_FATAL( "pStmt is NULL");
565 rc = sqlite3_finalize(pStmt);
566 if (rc != SQLITE_OK) {
567 ACCOUNT_FATAL( "sqlite3_finalize fail, rc : %d, db_error : %s\n", rc, _account_db_err_msg());
570 ACCOUNT_VERBOSE( "sqlite3_finalize finish");
573 static int _account_query_step(account_stmt pStmt)
575 assert(NULL != pStmt);
576 return sqlite3_step(pStmt);
581 static int _do_account_owner_existance_check()
583 /* TODO check owner*/
584 return ACCOUNT_ERROR_NONE;
587 static int _account_execute_insert_query(account_s *account)
590 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
591 int error_code = ACCOUNT_ERROR_NONE;
592 account_stmt hstmt = NULL;
594 if (!account->package_name) {
595 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
596 return ACCOUNT_ERROR_INVALID_PARAMETER;
599 if (!account->user_name && !account->display_name && !account->email_address) {
600 ACCOUNT_ERROR("Mandetory fields is NULL. At least one field is required among username, display name, email address\n");
601 return ACCOUNT_ERROR_INVALID_PARAMETER;
604 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
605 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s( user_name, email_address , display_name , icon_path , source , package_name , "
606 "access_token , domain_name , auth_type , secret , sync_support , txt_custom0, txt_custom1, txt_custom2, txt_custom3, txt_custom4, "
607 "int_custom0, int_custom1, int_custom2, int_custom3, int_custom4, txt_custom0 ) values "
608 "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? , ?, ?)", ACCOUNT_TABLE);
610 hstmt = _account_prepare_query(query);
611 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
613 _account_convert_account_to_sql(account, hstmt, query);
615 rc = _account_query_step(hstmt);
616 if (rc != SQLITE_DONE) {
617 ACCOUNT_FATAL( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
618 error_code = ACCOUNT_ERROR_DB_FAILED;
621 _account_query_finalize(hstmt);
627 static int _account_insert_capability(account_s *account, int account_id)
630 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
631 account_stmt hstmt = NULL;
633 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
635 if (g_slist_length( account->capablity_list)==0) {
636 ACCOUNT_ERROR( "_account_insert_capability, no capability\n");
637 return ACCOUNT_ERROR_NONE;
640 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
642 rc = _account_get_record_count(query);
645 ACCOUNT_WARNING( "_account_insert_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
646 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
653 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
656 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
657 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
658 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
660 hstmt = _account_prepare_query(query);
662 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
664 account_capability_s* cap_data = NULL;
665 cap_data = (account_capability_s*)iter->data;
666 ACCOUNT_VERBOSE("cap_data->type = %d, cap_data->value = %d \n", cap_data->type, cap_data->value);
668 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
669 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
670 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
671 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
672 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
673 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
674 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
675 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
676 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
677 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
679 rc = _account_query_step(hstmt);
681 if (rc != SQLITE_DONE) {
682 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
686 _account_query_finalize(hstmt);
691 ACCOUNT_VERBOSE( "_account_insert_capability() DONE\n");
693 return ACCOUNT_ERROR_NONE;
696 static int _account_update_capability(account_s *account, int account_id)
699 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
700 account_stmt hstmt = NULL;
702 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
704 if (g_slist_length( account->capablity_list)==0) {
705 ACCOUNT_ERROR( "_account_insert_capability, no capability\n");
706 return ACCOUNT_ERROR_NONE;
709 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
711 rc = _account_get_record_count(query);
714 ACCOUNT_WARNING( "_account_insert_capability : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
715 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
718 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
720 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id=? ", CAPABILITY_TABLE);
721 hstmt = _account_prepare_query(query);
723 _account_query_bind_int(hstmt, count++, (int)account_id);
724 rc = _account_query_step(hstmt);
726 if (rc != SQLITE_DONE) {
727 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
728 return ACCOUNT_ERROR_DB_FAILED;
730 _account_query_finalize(hstmt);
735 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
738 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
739 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
740 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
742 hstmt = _account_prepare_query(query);
744 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
746 account_capability_s* cap_data = NULL;
747 cap_data = (account_capability_s*)iter->data;
749 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
750 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
751 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
752 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
753 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
754 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
755 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
756 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
757 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
758 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
760 rc = _account_query_step(hstmt);
762 if (rc != SQLITE_DONE) {
763 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
767 _account_query_finalize(hstmt);
772 ACCOUNT_DEBUG( "_account_insert_capability() DONE\n");
774 return ACCOUNT_ERROR_NONE;
777 static int _account_update_capability_by_user_name(account_s *account, char *user_name, char *package_name )
780 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
781 account_stmt hstmt = NULL;
783 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
785 if (g_slist_length( account->capablity_list)==0) {
786 ACCOUNT_ERROR( "_account_insert_capability, no capability\n");
787 return ACCOUNT_ERROR_NONE;
790 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where package_name=%s and user_name=%s", ACCOUNT_TABLE, package_name, user_name);
792 rc = _account_get_record_count(query);
795 ACCOUNT_WARNING( "_account_insert_capability : related account item is not existed rc=%d , %s ", rc, _account_db_err_msg());
796 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
799 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
801 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name=? and user_name=? ", CAPABILITY_TABLE);
802 hstmt = _account_prepare_query(query);
804 _account_query_bind_text(hstmt, count++, (char*)account->package_name);
805 _account_query_bind_text(hstmt, count++, (char*)account->user_name);
806 rc = _account_query_step(hstmt);
807 if (rc != SQLITE_DONE) {
808 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
809 return ACCOUNT_ERROR_DB_FAILED;
812 _account_query_finalize(hstmt);
817 for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
820 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
821 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(key, value, package_name, user_name, account_id) VALUES "
822 "(?, ?, ?, ?, ?) ", CAPABILITY_TABLE);
824 hstmt = _account_prepare_query(query);
826 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
828 account_capability_s* cap_data = NULL;
829 cap_data = (account_capability_s*)iter->data;
831 ret = _account_query_bind_text(hstmt, count++, cap_data->type);
832 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
833 ret = _account_query_bind_int(hstmt, count++, cap_data->value);
834 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
835 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
836 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
837 ret = _account_query_bind_text(hstmt, count++, (char*)account->user_name);
838 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
839 ret = _account_query_bind_int(hstmt, count++, (int)account->id);
840 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Integer binding fail"));
842 rc = _account_query_step(hstmt);
844 if (rc != SQLITE_DONE) {
845 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
849 _account_query_finalize(hstmt);
854 ACCOUNT_VERBOSE( "_account_insert_capability() DONE\n");
856 return ACCOUNT_ERROR_NONE;
859 static int _account_query_table_column_int(account_stmt pStmt, int pos)
861 assert(NULL != pStmt);
863 return sqlite3_column_int(pStmt, pos);
866 static char *_account_query_table_column_text(account_stmt pStmt, int pos)
868 assert(NULL != pStmt);
870 return (char *)sqlite3_column_text(pStmt, pos);
873 static void _account_db_data_to_text(char *textbuf, char **output)
875 if (textbuf && strlen(textbuf)>0) {
880 *output = strdup(textbuf);
882 ACCOUNT_WARNING("_account_db_data_to_text : no text");
886 static void _account_convert_column_to_account(account_stmt hstmt, account_s *account_record)
888 char *textbuf = NULL;
890 account_record->id = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_ID);
892 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_NAME);
893 _account_db_data_to_text(textbuf, &(account_record->user_name));
895 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_EMAIL_ADDRESS);
896 _account_db_data_to_text(textbuf, &(account_record->email_address));
898 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DISPLAY_NAME);
899 _account_db_data_to_text(textbuf, &(account_record->display_name));
901 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ICON_PATH);
902 _account_db_data_to_text(textbuf, &(account_record->icon_path));
904 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_SOURCE);
905 _account_db_data_to_text(textbuf, &(account_record->source));
907 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_PACKAGE_NAME);
908 _account_db_data_to_text(textbuf, &(account_record->package_name));
910 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ACCESS_TOKEN);
911 _account_db_data_to_text(textbuf, &(account_record->access_token));
913 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DOMAIN_NAME);
914 _account_db_data_to_text(textbuf, &(account_record->domain_name));
916 account_record->auth_type = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_AUTH_TYPE);
918 account_record->secret = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SECRET);
920 account_record->sync_support = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SYNC_SUPPORT);
922 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_0);
923 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[0]));
925 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_1);
926 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[1]));
928 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_2);
929 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[2]));
931 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_3);
932 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[3]));
934 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_TEXT_4);
935 _account_db_data_to_text(textbuf, &(account_record->user_data_txt[4]));
937 account_record->user_data_int[0] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_0);
938 account_record->user_data_int[1] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_1);
939 account_record->user_data_int[2] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_2);
940 account_record->user_data_int[3] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_3);
941 account_record->user_data_int[4] = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_USER_INT_4);
943 ACCOUNT_VERBOSE("END _account_convert_column_to_account");
946 static void _account_convert_column_to_capability(account_stmt hstmt, account_capability_s *capability_record)
948 char *textbuf = NULL;
950 capability_record->id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ID);
952 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_KEY);
953 _account_db_data_to_text(textbuf, &(capability_record->type));
955 capability_record->value = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_VALUE);
957 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_PACKAGE_NAME);
958 _account_db_data_to_text(textbuf, &(capability_record->package_name));
960 textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_USER_NAME);
961 _account_db_data_to_text(textbuf, &(capability_record->user_name));
963 capability_record->account_id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ACCOUNT_ID);
965 ACCOUNT_VERBOSE("END _account_convert_column_to_capability");
968 static void _account_convert_column_to_custom(account_stmt hstmt, account_custom_s *custom_record)
970 char *textbuf = NULL;
972 custom_record->account_id = _account_query_table_column_int(hstmt, ACCOUNT_CUSTOM_FIELD_ACCOUNT_ID);
974 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_APP_ID);
975 _account_db_data_to_text(textbuf, &(custom_record->app_id));
977 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_KEY);
978 _account_db_data_to_text(textbuf, &(custom_record->key));
980 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_VALUE);
981 _account_db_data_to_text(textbuf, &(custom_record->value));
983 ACCOUNT_VERBOSE("END _account_convert_column_to_custom");
986 bool _account_get_capability_text_cb(const char* capability_type, account_capability_state_e capability_value, void *user_data)
988 account_s *data = (account_s*)user_data;
990 account_capability_s *cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
992 if (cap_data == NULL)
994 ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
996 cap_data->type = _account_get_text(capability_type);
997 cap_data->value = capability_value;
999 data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data);
1001 ACCOUNT_VERBOSE("_account_get_capability_text_cb :: %d\n", capability_type);
1007 bool _account_get_custom_text_cb(char* key, char* value, void *user_data)
1009 account_s *data = (account_s*)user_data;
1011 account_custom_s *custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
1013 if (custom_data == NULL) {
1014 ACCOUNT_DEBUG("_account_get_custom_text_cb :: malloc fail\n");
1017 ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
1019 custom_data->account_id = data->id;
1020 custom_data->app_id = _account_get_text(data->package_name);
1021 custom_data->key = _account_get_text(key);
1022 custom_data->value = _account_get_text(value);
1024 data->custom_list = g_slist_append(data->custom_list, (gpointer)custom_data);
1030 static char *_account_get_text(const char *text_data)
1032 char *text_value = NULL;
1034 if (text_data != NULL) {
1035 text_value = strdup(text_data);
1036 ACCOUNT_VERBOSE("text_value = %s", text_value);
1041 static int _account_update_account_by_user_name(account_s *account, char *user_name, char *package_name)
1043 int rc = 0, binding_count = 0, count = 0;
1044 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1045 int error_code = ACCOUNT_ERROR_NONE;
1046 account_stmt hstmt = NULL;
1048 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is NULL.\n"));
1049 ACCOUNT_RETURN_VAL((package_name!= NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is NULL.\n"));
1051 if (!account->user_name && !account->display_name && !account->email_address) {
1052 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
1053 return ACCOUNT_ERROR_INVALID_PARAMETER;
1056 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE user_name='%s' and package_name='%s'"
1057 , ACCOUNT_TABLE, user_name, package_name);
1059 count = _account_get_record_count(query);
1061 ACCOUNT_INFO("_account_update_account_by_user_name : The account not exist!, count = %d, user_name=%s, package_name=%s\n",
1062 count, user_name, package_name);
1063 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1066 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
1067 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
1068 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
1069 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
1070 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE user_name=? and package_name=? ", ACCOUNT_TABLE);
1072 hstmt = _account_prepare_query(query);
1073 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
1075 binding_count = _account_convert_account_to_sql(account, hstmt, query);
1077 _account_query_bind_text(hstmt, binding_count++, user_name);
1078 _account_query_bind_text(hstmt, binding_count++, package_name);
1079 rc = _account_query_step(hstmt);
1080 if (rc != SQLITE_DONE) {
1081 ACCOUNT_FATAL( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
1083 _account_query_finalize(hstmt);
1086 /*update capability*/
1087 error_code = _account_update_capability_by_user_name(account, user_name, package_name);
1092 int account_insert_to_db(account_h account, int *account_id)
1094 int error_code = ACCOUNT_ERROR_NONE;
1096 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1097 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1098 ACCOUNT_RETURN_VAL((account_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID POINTER IS NULL"));
1100 account_s *data = (account_s*)account;
1102 pthread_mutex_lock(&account_mutex);
1104 if (_account_check_duplicated(data)) {
1105 error_code = ACCOUNT_ERROR_DUPLICATED;
1107 *account_id = _account_get_next_sequence(ACCOUNT_TABLE);
1109 error_code = _account_execute_insert_query(data);
1111 if (error_code != ACCOUNT_ERROR_NONE)
1115 ACCOUNT_VERBOSE( "_account_execute_insert_query, insert error_code : %d", error_code);
1117 _account_insert_capability(data, *account_id);
1118 _account_insert_custom(data, *account_id);
1120 pthread_mutex_unlock(&account_mutex);
1121 _account_insert_delete_update_notification_send(ACCOUNT_NOTI_NAME_INSERT);
1123 return ACCOUNT_ERROR_NONE;
1127 int account_create(account_h *account)
1130 ACCOUNT_ERROR("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
1131 return ACCOUNT_ERROR_INVALID_PARAMETER;
1134 account_s *data = (account_s*)malloc(sizeof(account_s));
1137 ACCOUNT_FATAL("Memory Allocation Failed");
1138 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1140 ACCOUNT_MEMSET(data, 0, sizeof(account_s));
1142 ACCOUNT_VERBOSE("create handle=%p\n", *account);
1144 /*Setting account as visible by default*/
1145 data->secret = ACCOUNT_SECRECY_VISIBLE;
1147 /*Setting account as not supporting sync by default*/
1148 data->sync_support = ACCOUNT_SYNC_NOT_SUPPORT;
1150 *account = (account_h)data;
1152 return ACCOUNT_ERROR_NONE;
1155 int account_set_user_name(account_h account, const char *user_name)
1158 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1159 return ACCOUNT_ERROR_INVALID_PARAMETER;
1163 ACCOUNT_ERROR("(%s)-(%d) user_name is NULL.\n", __FUNCTION__, __LINE__);
1164 return ACCOUNT_ERROR_INVALID_PARAMETER;
1167 account_s *data = (account_s*)account;
1169 _ACCOUNT_FREE(data->user_name);
1170 data->user_name = _account_get_text(user_name);
1172 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->user_name, user_name);
1174 return ACCOUNT_ERROR_NONE;
1177 int account_set_display_name(account_h account, const char *display_name)
1180 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1181 return ACCOUNT_ERROR_INVALID_PARAMETER;
1184 if (!display_name) {
1185 ACCOUNT_ERROR("(%s)-(%d) display_name is NULL.\n", __FUNCTION__, __LINE__);
1186 return ACCOUNT_ERROR_INVALID_PARAMETER;
1189 account_s *data = (account_s*)account;
1191 _ACCOUNT_FREE(data->display_name);
1192 data->display_name = _account_get_text(display_name);
1194 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->display_name, display_name);
1196 return ACCOUNT_ERROR_NONE;
1199 int account_set_email_address(account_h account, const char *email_address)
1202 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1203 return ACCOUNT_ERROR_INVALID_PARAMETER;
1206 if (!email_address) {
1207 ACCOUNT_ERROR("(%s)-(%d) email_address is NULL.\n", __FUNCTION__, __LINE__);
1208 return ACCOUNT_ERROR_INVALID_PARAMETER;
1211 account_s *data = (account_s*)account;
1213 _ACCOUNT_FREE(data->email_address);
1214 data->email_address = _account_get_text(email_address);
1216 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->email_address, email_address);
1218 return ACCOUNT_ERROR_NONE;
1221 int account_set_icon_path(account_h account, const char *icon_path)
1224 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1225 return ACCOUNT_ERROR_INVALID_PARAMETER;
1229 ACCOUNT_ERROR("(%s)-(%d) icon_path is NULL.\n", __FUNCTION__, __LINE__);
1230 return ACCOUNT_ERROR_INVALID_PARAMETER;
1233 account_s *data = (account_s*)account;
1235 _ACCOUNT_FREE(data->icon_path);
1236 data->icon_path = _account_get_text(icon_path);
1238 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->icon_path, icon_path);
1240 return ACCOUNT_ERROR_NONE;
1243 int account_set_source(account_h account, const char *source)
1246 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1247 return ACCOUNT_ERROR_INVALID_PARAMETER;
1251 ACCOUNT_ERROR("(%s)-(%d) source is NULL.\n", __FUNCTION__, __LINE__);
1252 return ACCOUNT_ERROR_INVALID_PARAMETER;
1254 account_s *data = (account_s*)account;
1256 _ACCOUNT_FREE(data->source);
1257 data->source = _account_get_text(source);
1259 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->source, source);
1261 return ACCOUNT_ERROR_NONE;
1264 int account_set_package_name(account_h account, const char *package_name)
1267 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1268 return ACCOUNT_ERROR_INVALID_PARAMETER;
1271 if (!package_name) {
1272 ACCOUNT_ERROR("(%s)-(%d) package_name is NULL.\n", __FUNCTION__, __LINE__);
1273 return ACCOUNT_ERROR_INVALID_PARAMETER;
1276 account_s *data = (account_s*)account;
1278 _ACCOUNT_FREE(data->package_name);
1279 data->package_name = _account_get_text(package_name);
1281 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->package_name, package_name);
1283 return ACCOUNT_ERROR_NONE;
1286 int account_set_domain_name(account_h account, const char *domain_name)
1289 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1290 return ACCOUNT_ERROR_INVALID_PARAMETER;
1294 ACCOUNT_ERROR("(%s)-(%d) domain_name is NULL.\n", __FUNCTION__, __LINE__);
1295 return ACCOUNT_ERROR_INVALID_PARAMETER;
1297 account_s *data = (account_s*)account;
1299 _ACCOUNT_FREE(data->domain_name);
1300 data->domain_name = _account_get_text(domain_name);
1302 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->domain_name, domain_name);
1304 return ACCOUNT_ERROR_NONE;
1307 int account_set_access_token(account_h account, const char *access_token)
1310 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1311 return ACCOUNT_ERROR_INVALID_PARAMETER;
1314 if (!access_token) {
1315 ACCOUNT_ERROR("(%s)-(%d) access_token is NULL.\n", __FUNCTION__, __LINE__);
1316 return ACCOUNT_ERROR_INVALID_PARAMETER;
1319 account_s *data = (account_s*)account;
1321 _ACCOUNT_FREE(data->access_token);
1322 data->access_token = _account_get_text(access_token);
1324 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->access_token, access_token);
1326 return ACCOUNT_ERROR_NONE;
1329 int account_set_user_text(account_h account, int index, const char *user_txt)
1332 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1333 return ACCOUNT_ERROR_INVALID_PARAMETER;
1337 ACCOUNT_ERROR("(%s)-(%d) user_txt is NULL.\n", __FUNCTION__, __LINE__);
1338 return ACCOUNT_ERROR_INVALID_PARAMETER;
1340 if (index >= USER_TXT_CNT || index < 0) {
1341 ACCOUNT_ERROR("(%s)-(%d) index rage should be between 0-4.\n", __FUNCTION__, __LINE__);
1342 return ACCOUNT_ERROR_INVALID_PARAMETER;
1345 account_s *data = (account_s*)account;
1347 _ACCOUNT_FREE(data->user_data_txt[index]);
1348 data->user_data_txt[index] = _account_get_text(user_txt);
1350 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->user_data_txt[index], user_txt);
1352 return ACCOUNT_ERROR_NONE;
1355 int account_set_custom(account_h account, const char* key, const char* value)
1358 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1359 return ACCOUNT_ERROR_INVALID_PARAMETER;
1363 ACCOUNT_ERROR("(%s)-(%d) key is NULL.\n", __FUNCTION__, __LINE__);
1364 return ACCOUNT_ERROR_INVALID_PARAMETER;
1368 ACCOUNT_ERROR("(%s)-(%d) value is NULL.\n", __FUNCTION__, __LINE__);
1369 return ACCOUNT_ERROR_INVALID_PARAMETER;
1372 account_s *data = (account_s*)account;
1375 bool b_is_new = TRUE;
1377 for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) {
1379 account_custom_s* custom_data = NULL;
1380 custom_data = (account_custom_s*)iter->data;
1381 ACCOUNT_VERBOSE( "account_custom_s->key = %s, account_custom_s->value = %s \n", custom_data->key, custom_data->value);
1383 if(!strcmp(custom_data->key, key)) {
1384 _ACCOUNT_FREE(custom_data->value);
1385 custom_data->value = _account_get_text(value);
1391 account_custom_s* custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
1393 if (custom_data == NULL) {
1394 ACCOUNT_FATAL("(%s)-(%d) MALLOC FAIL\n", __FUNCTION__, __LINE__);
1395 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1397 ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
1398 custom_data->account_id = data->id;
1399 custom_data->app_id = _account_get_text(data->package_name);
1400 custom_data->key = _account_get_text(key);
1401 custom_data->value = _account_get_text(value);
1402 data->custom_list = g_slist_append(data->custom_list, (gpointer)custom_data);
1405 return ACCOUNT_ERROR_NONE;
1408 int account_set_auth_type(account_h account, const account_auth_type_e auth_type)
1410 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
1412 if ( (auth_type < 0) || (auth_type > ACCOUNT_AUTH_TYPE_CLIENT_LOGIN)) {
1413 ACCOUNT_ERROR("(%s)-(%d) auth_type is less than 1 or more than enum max.\n", __FUNCTION__, __LINE__);
1414 return ACCOUNT_ERROR_INVALID_PARAMETER;
1417 account_s *data = (account_s*)account;
1419 data->auth_type = (int)auth_type;
1421 return ACCOUNT_ERROR_NONE;
1424 int account_set_secret(account_h account, const account_secrecy_state_e secret)
1426 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
1428 if ( (secret < 0) || (secret > ACCOUNT_SECRECY_VISIBLE)) {
1429 ACCOUNT_ERROR("(%s)-(%d) auth_type is less than 1 or more than enum max.\n", __FUNCTION__, __LINE__);
1430 return ACCOUNT_ERROR_INVALID_PARAMETER;
1433 account_s *data = (account_s*)account;
1435 data->secret = (int)secret;
1437 return ACCOUNT_ERROR_NONE;
1440 int account_set_sync_support(account_h account, const account_sync_state_e sync_support)
1442 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
1444 if ( (sync_support < 0) || (sync_support > ACCOUNT_SUPPORTS_SYNC)) {
1445 ACCOUNT_ERROR("(%s)-(%d) sync_support is less than 1 or more than enum max.\n", __FUNCTION__, __LINE__);
1446 return ACCOUNT_ERROR_INVALID_PARAMETER;
1449 account_s *data = (account_s*)account;
1451 data->sync_support= (int)sync_support;
1453 return ACCOUNT_ERROR_NONE;
1456 int account_set_user_int(account_h account, int index, const int user_int)
1459 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1460 return ACCOUNT_ERROR_INVALID_PARAMETER;
1463 if (index >= USER_INT_CNT ||index < 0) {
1464 ACCOUNT_ERROR("(%s)-(%d) index rage should be between 0-4.\n", __FUNCTION__, __LINE__);
1465 return ACCOUNT_ERROR_INVALID_PARAMETER;
1468 account_s *data = (account_s*)account;
1470 data->user_data_int[index] = user_int;
1472 return ACCOUNT_ERROR_NONE;
1475 int account_set_capability(account_h account, const char* capability_type, account_capability_state_e capability_value)
1477 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("account handle is null"));
1478 ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type is null"));
1480 if ((capability_value != ACCOUNT_CAPABILITY_DISABLED) && (capability_value != ACCOUNT_CAPABILITY_ENABLED)) {
1481 ACCOUNT_ERROR("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
1482 return ACCOUNT_ERROR_INVALID_PARAMETER;
1485 account_s *data = (account_s*)account;
1487 GSList *iter = NULL;
1488 bool b_is_new = TRUE;
1490 for(iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1491 account_capability_s *cap_data = NULL;
1492 cap_data = (account_capability_s*)iter->data;
1494 if(!strcmp(cap_data->type, capability_type)) {
1495 cap_data->value = capability_value;
1502 account_capability_s* cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
1504 if (cap_data == NULL)
1505 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1506 ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
1508 cap_data->type = _account_get_text(capability_type);
1509 cap_data->value = capability_value;
1510 data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data);
1513 return ACCOUNT_ERROR_NONE;
1516 int account_get_user_name(account_h account, char **user_name)
1519 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1520 return ACCOUNT_ERROR_INVALID_PARAMETER;
1524 ACCOUNT_ERROR("(%s)-(%d) user name is NULL.\n", __FUNCTION__, __LINE__);
1525 return ACCOUNT_ERROR_INVALID_PARAMETER;
1528 account_s *data = (account_s*)account;
1530 (*user_name) = NULL;
1531 *user_name = _account_get_text(data->user_name);
1533 return ACCOUNT_ERROR_NONE;
1536 int account_get_display_name(account_h account, char **display_name)
1539 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1540 return ACCOUNT_ERROR_INVALID_PARAMETER;
1543 if (!display_name) {
1544 ACCOUNT_ERROR("(%s)-(%d) display name is NULL.\n", __FUNCTION__, __LINE__);
1545 return ACCOUNT_ERROR_INVALID_PARAMETER;
1548 account_s *data = (account_s*)account;
1550 (*display_name) = NULL;
1552 *display_name = _account_get_text(data->display_name);
1554 return ACCOUNT_ERROR_NONE;
1557 int account_get_email_address(account_h account,char **email_address)
1560 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1561 return ACCOUNT_ERROR_INVALID_PARAMETER;
1564 if (!email_address) {
1565 ACCOUNT_ERROR("(%s)-(%d) email address is NULL.\n", __FUNCTION__, __LINE__);
1566 return ACCOUNT_ERROR_INVALID_PARAMETER;
1569 account_s *data = (account_s*)account;
1571 (*email_address) = NULL;
1573 *email_address = _account_get_text(data->email_address);
1575 return ACCOUNT_ERROR_NONE;
1578 int account_get_icon_path(account_h account, char **icon_path)
1581 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1582 return ACCOUNT_ERROR_INVALID_PARAMETER;
1586 ACCOUNT_ERROR("(%s)-(%d) icon path is NULL.\n", __FUNCTION__, __LINE__);
1587 return ACCOUNT_ERROR_INVALID_PARAMETER;
1590 account_s *data = (account_s*)account;
1592 (*icon_path) = NULL;
1594 *icon_path = _account_get_text(data->icon_path);
1596 return ACCOUNT_ERROR_NONE;
1599 int account_get_source(account_h account, char **source)
1602 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1603 return ACCOUNT_ERROR_INVALID_PARAMETER;
1607 ACCOUNT_ERROR("(%s)-(%d) source is NULL.\n", __FUNCTION__, __LINE__);
1608 return ACCOUNT_ERROR_INVALID_PARAMETER;
1611 account_s *data = (account_s*)account;
1615 *source = _account_get_text(data->source);
1617 return ACCOUNT_ERROR_NONE;
1620 int account_get_package_name(account_h account, char **package_name)
1623 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1624 return ACCOUNT_ERROR_INVALID_PARAMETER;
1627 if (!package_name) {
1628 ACCOUNT_ERROR("(%s)-(%d) package name is NULL.\n", __FUNCTION__, __LINE__);
1629 return ACCOUNT_ERROR_INVALID_PARAMETER;
1632 account_s *data = (account_s*)account;
1634 (*package_name) = NULL;
1636 *package_name = _account_get_text(data->package_name);
1638 return ACCOUNT_ERROR_NONE;
1641 int account_get_domain_name(account_h account, char **domain_name)
1644 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1645 return ACCOUNT_ERROR_INVALID_PARAMETER;
1649 ACCOUNT_ERROR("(%s)-(%d) domain name is NULL.\n", __FUNCTION__, __LINE__);
1650 return ACCOUNT_ERROR_INVALID_PARAMETER;
1653 account_s *data = (account_s*)account;
1655 (*domain_name) = NULL;
1657 *domain_name = _account_get_text(data->domain_name);
1659 return ACCOUNT_ERROR_NONE;
1662 int account_get_access_token(account_h account, char **access_token)
1665 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1666 return ACCOUNT_ERROR_INVALID_PARAMETER;
1669 if (!access_token) {
1670 ACCOUNT_ERROR("(%s)-(%d) access token is NULL.\n", __FUNCTION__, __LINE__);
1671 return ACCOUNT_ERROR_INVALID_PARAMETER;
1674 account_s *data = (account_s*)account;
1676 (*access_token) = NULL;
1678 *access_token = _account_get_text(data->access_token);
1680 return ACCOUNT_ERROR_NONE;
1683 int account_get_user_text(account_h account, int user_text_index, char **text)
1686 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1687 return ACCOUNT_ERROR_INVALID_PARAMETER;
1691 ACCOUNT_ERROR("(%s)-(%d) text is NULL.\n", __FUNCTION__, __LINE__);
1692 return ACCOUNT_ERROR_INVALID_PARAMETER;
1694 ACCOUNT_RETURN_VAL((user_text_index >=0 && user_text_index < USER_TXT_CNT ), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID USER TEXT INDEX"));
1696 account_s *data = (account_s*)account;
1700 *text = _account_get_text(data->user_data_txt[user_text_index]);
1702 return ACCOUNT_ERROR_NONE;
1705 int account_get_auth_type(account_h account, account_auth_type_e *auth_type)
1708 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1709 return ACCOUNT_ERROR_INVALID_PARAMETER;
1712 ACCOUNT_ERROR("(%s)-(%d) auth_type is NULL.\n", __FUNCTION__, __LINE__);
1713 return ACCOUNT_ERROR_INVALID_PARAMETER;
1716 account_s* data = (account_s*)account;
1718 *auth_type = data->auth_type;
1720 return ACCOUNT_ERROR_NONE;
1723 int account_get_secret(account_h account, account_secrecy_state_e *secret)
1726 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1727 return ACCOUNT_ERROR_INVALID_PARAMETER;
1730 ACCOUNT_ERROR("(%s)-(%d) secret is NULL.\n", __FUNCTION__, __LINE__);
1731 return ACCOUNT_ERROR_INVALID_PARAMETER;
1734 account_s* data = (account_s*)account;
1736 *secret = data->secret;
1738 return ACCOUNT_ERROR_NONE;
1741 int account_get_sync_support(account_h account, account_sync_state_e *sync_support)
1744 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1745 return ACCOUNT_ERROR_INVALID_PARAMETER;
1747 if (!sync_support) {
1748 ACCOUNT_ERROR("(%s)-(%d) sync_support is NULL.\n", __FUNCTION__, __LINE__);
1749 return ACCOUNT_ERROR_INVALID_PARAMETER;
1752 account_s* data = (account_s*)account;
1754 *sync_support = data->sync_support;
1756 return ACCOUNT_ERROR_NONE;
1759 int account_get_account_id(account_h account, int *account_id)
1762 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1763 return ACCOUNT_ERROR_INVALID_PARAMETER;
1766 ACCOUNT_ERROR("(%s)-(%d) account_id is NULL.\n", __FUNCTION__, __LINE__);
1767 return ACCOUNT_ERROR_INVALID_PARAMETER;
1770 account_s *data = (account_s*)account;
1772 *account_id = data->id;
1774 return ACCOUNT_ERROR_NONE;
1777 int account_get_user_int(account_h account, int user_int_index, int *integer)
1780 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1781 return ACCOUNT_ERROR_INVALID_PARAMETER;
1784 ACCOUNT_RETURN_VAL((user_int_index >=0 && user_int_index < USER_TXT_CNT ), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID USER TEXT INDEX"));
1787 ACCOUNT_ERROR("(%s)-(%d) integer is NULL.\n", __FUNCTION__, __LINE__);
1788 return ACCOUNT_ERROR_INVALID_PARAMETER;
1791 account_s *data = (account_s*)account;
1793 *integer = data->user_data_int[user_int_index];
1795 return ACCOUNT_ERROR_NONE;
1798 int account_get_capability(account_h account, const char* capability_type, account_capability_state_e* capability_value)
1800 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1801 ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type is NULL"));
1802 ACCOUNT_RETURN_VAL((capability_value != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_value is NULL"));
1805 account_s *data = (account_s*)account;
1807 for (iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1808 account_capability_s *cap_data = NULL;
1810 cap_data = (account_capability_s*)iter->data;
1812 ACCOUNT_VERBOSE("account_get_capability :: type = %d, value = %d", cap_data->type, cap_data->value);
1814 if(!strcmp(capability_type, cap_data->type)) {
1815 *capability_value = cap_data->value;
1816 return ACCOUNT_ERROR_NONE;
1820 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1823 int account_get_capability_all(account_h account, capability_cb cb_func, void *user_data)
1825 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1826 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
1829 account_s *data = (account_s*)account;
1831 for (iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1832 account_capability_s *cap_data = NULL;
1834 cap_data = (account_capability_s*)iter->data;
1836 ACCOUNT_VERBOSE("account_get_capability :: type = %d, value = %d", cap_data->type, cap_data->value);
1838 cb_func(cap_data->type, cap_data->value, user_data);
1841 return ACCOUNT_ERROR_NONE;
1844 int account_get_custom(account_h account, const char* key, char** value)
1846 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1847 ACCOUNT_RETURN_VAL((key != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO KEY TO REQUEST"));
1848 ACCOUNT_RETURN_VAL((value != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("VALUE POINTER IS NULL"));
1851 account_s *data = (account_s*)account;
1853 for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) {
1854 account_custom_s *custom_data = NULL;
1856 custom_data = (account_custom_s*)iter->data;
1858 ACCOUNT_VERBOSE("account_get_custom :: key = %s, value = %s", custom_data->key, custom_data->value);
1860 if(!strcmp(key, custom_data->key)) {
1862 *value = _account_get_text(custom_data->value);
1863 return ACCOUNT_ERROR_NONE;
1867 ACCOUNT_INFO("key is not found %s", key);
1869 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1872 int account_get_custom_all(account_h account, account_custom_cb cb_func, void* user_data)
1874 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
1875 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
1878 account_s *data = (account_s*)account;
1880 for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) {
1881 bool cb_ret = FALSE;
1882 account_custom_s *custom_data = NULL;
1884 custom_data = (account_custom_s*)iter->data;
1886 ACCOUNT_VERBOSE("account_get_custom :: key = %s, value = %s", custom_data->key, custom_data->value);
1888 cb_ret = cb_func(custom_data->key, custom_data->value, user_data);
1890 ACCOUNT_INFO("account_get_custom_all callback func ret = %d", cb_ret);
1895 return ACCOUNT_ERROR_NONE;
1898 int account_query_capability_by_account_id(capability_cb cb_func, int account_id, void *user_data )
1900 int error_code = ACCOUNT_ERROR_NONE;
1901 account_stmt hstmt = NULL;
1902 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1904 int ret = ACCOUNT_ERROR_NONE;
1906 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1907 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
1908 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1910 ret = _do_account_owner_existance_check();
1911 if (ret != ACCOUNT_ERROR_NONE) {
1912 ACCOUNT_ERROR("_do_account_owner_existance_check Failed !!!\n");
1915 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1917 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
1918 hstmt = _account_prepare_query(query);
1920 rc = _account_query_step(hstmt);
1921 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1923 account_capability_s* capability_record = NULL;
1925 while (rc == SQLITE_ROW) {
1926 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
1928 if (capability_record == NULL) {
1929 ACCOUNT_FATAL("malloc Failed");
1933 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
1935 _account_convert_column_to_capability(hstmt, capability_record);
1937 cb_func(capability_record->type, capability_record->value, user_data);
1939 _account_free_capability_items(capability_record);
1940 _ACCOUNT_FREE(capability_record);
1942 rc = _account_query_step(hstmt);
1945 _account_query_finalize(hstmt);
1948 error_code = ACCOUNT_ERROR_NONE;
1951 if (hstmt != NULL) {
1952 _account_query_finalize(hstmt);
1956 pthread_mutex_unlock(&account_mutex);
1963 static int _account_compare_old_record(account_s *new_account, int account_id)
1965 int error_code = ACCOUNT_ERROR_NONE;
1966 account_stmt hstmt = NULL;
1967 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
1969 account_s *old_account = NULL;
1971 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
1972 ACCOUNT_RETURN_VAL((new_account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
1973 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
1975 old_account = (account_s*)calloc(1, sizeof(account_s));
1977 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1979 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
1980 hstmt = _account_prepare_query(query);
1982 rc = _account_query_step(hstmt);
1983 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1985 while (rc == SQLITE_ROW) {
1986 _account_convert_column_to_account(hstmt, old_account);
1987 ACCOUNT_VERBOSE("get account info by id %p\n", old_account);
1988 rc = _account_query_step(hstmt);
1991 _account_query_finalize(hstmt);
1995 error_code = account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account);
1998 error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account);
2004 if(!new_account->user_name) {
2005 if(old_account->user_name)
2006 new_account->user_name = _account_get_text(old_account->user_name);
2010 if(!new_account->display_name) {
2011 if(old_account->display_name)
2012 new_account->display_name = _account_get_text(old_account->display_name);
2016 if(!new_account->email_address) {
2017 if(old_account->email_address)
2018 new_account->email_address = _account_get_text(old_account->email_address);
2022 if(!new_account->icon_path) {
2023 if(old_account->icon_path)
2024 new_account->icon_path = _account_get_text(old_account->icon_path);
2028 if(!new_account->source) {
2029 if(old_account->source)
2030 new_account->source = _account_get_text(old_account->source);
2034 if(!new_account->package_name) {
2035 if(old_account->package_name)
2036 new_account->package_name = _account_get_text(old_account->package_name);
2040 if(!new_account->access_token) {
2041 if(old_account->access_token)
2042 new_account->access_token = _account_get_text(old_account->access_token);
2047 for(i=0;i<USER_TXT_CNT;i++) {
2048 if(!new_account->user_data_txt[i]) {
2049 if(old_account->user_data_txt[i])
2050 new_account->user_data_txt[i] = _account_get_text(old_account->user_data_txt[i]);
2055 if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) {
2056 new_account->auth_type = old_account->auth_type;
2060 if(new_account->secret== ACCOUNT_SECRECY_INVALID) {
2061 new_account->secret = old_account->secret;
2065 if(new_account->sync_support == ACCOUNT_SYNC_INVALID) {
2066 new_account->sync_support = old_account->sync_support;
2070 for(i=0;i<USER_INT_CNT;i++) {
2071 if(new_account->user_data_int[i] == 0) {
2072 new_account->user_data_int[i] = old_account->user_data_int[i];
2078 // user custom table
2083 if (hstmt != NULL) {
2084 _account_query_finalize(hstmt);
2089 _account_free_account_items(old_account);
2090 _ACCOUNT_FREE(old_account);
2093 return ACCOUNT_ERROR_NONE;
2097 static int _account_update_account(account_s *account, int account_id)
2099 int rc = 0, binding_count =0;
2100 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2101 int error_code = ACCOUNT_ERROR_NONE, count=0;
2102 account_stmt hstmt = NULL;
2106 _account_compare_old_record(account, account_id);
2109 if (!account->package_name) {
2110 ACCOUNT_ERROR("Package name is mandetory field, it can not be NULL!!!!\n");
2111 return ACCOUNT_ERROR_INVALID_PARAMETER;
2114 if (!account->user_name && !account->display_name && !account->email_address) {
2115 ACCOUNT_ERROR("One field should be set among user name, display name, email address\n");
2116 return ACCOUNT_ERROR_INVALID_PARAMETER;
2119 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2121 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
2123 count = _account_get_record_count(query);
2125 ACCOUNT_WARNING(" Account record not found, count = %d\n", count);
2126 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2129 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2130 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET user_name=?, email_address =?, display_name =?, "
2131 "icon_path =?, source =?, package_name =? , access_token =?, domain_name =?, auth_type =?, secret =?, sync_support =?,"
2132 "txt_custom0=?, txt_custom1=?, txt_custom2=?, txt_custom3=?, txt_custom4=?, "
2133 "int_custom0=?, int_custom1=?, int_custom2=?, int_custom3=?, int_custom4=? WHERE _id=? ", ACCOUNT_TABLE);
2135 hstmt = _account_prepare_query(query);
2136 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
2138 binding_count = _account_convert_account_to_sql(account, hstmt, query);
2139 _account_query_bind_int(hstmt, binding_count++, account_id);
2141 rc = _account_query_step(hstmt);
2142 if (rc != SQLITE_DONE) {
2143 ACCOUNT_DEBUG( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
2146 _account_query_finalize(hstmt);
2149 /*update capability*/
2150 error_code = _account_update_capability(account, account_id);
2153 error_code = _account_update_custom(account, account_id);
2158 int account_update_to_db_by_id(const account_h account, int account_id)
2160 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
2161 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account id is not valid"));
2162 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2163 int error_code = ACCOUNT_ERROR_NONE;
2164 account_s* data = (account_s*)account;
2166 pthread_mutex_lock(&account_mutex);
2168 error_code = _account_update_account(data, account_id);
2170 if(error_code != ACCOUNT_ERROR_NONE) {
2171 pthread_mutex_unlock(&account_mutex);
2175 pthread_mutex_unlock(&account_mutex);
2176 _account_insert_delete_update_notification_send(ACCOUNT_NOTI_NAME_UPDATE);
2178 return ACCOUNT_ERROR_NONE;
2181 int account_update_to_db_by_user_name(account_h account, const char *user_name, const char *package_name)
2183 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
2184 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
2185 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
2186 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2188 int error_code = ACCOUNT_ERROR_NONE;
2189 account_s *data = (account_s*)account;
2191 pthread_mutex_lock(&account_mutex);
2193 error_code = _account_update_account_by_user_name(data, (char*)user_name, (char*)package_name);
2195 pthread_mutex_unlock(&account_mutex);
2196 _account_insert_delete_update_notification_send(ACCOUNT_NOTI_NAME_UPDATE);
2198 return ACCOUNT_ERROR_NONE;
2201 int account_foreach_account_from_db(account_cb callback, void *user_data)
2203 int error_code = ACCOUNT_ERROR_NONE;
2204 account_stmt hstmt = NULL;
2205 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2207 GList *account_list = NULL;
2208 int ret = ACCOUNT_ERROR_NONE;
2210 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INFO IS NULL"));
2211 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2213 ret = _do_account_owner_existance_check();
2214 if (ret != ACCOUNT_ERROR_NONE) {
2215 ACCOUNT_WARNING("_do_account_owner_existance_check Failed !!!\n");
2218 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2220 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TABLE);
2221 hstmt = _account_prepare_query(query);
2223 rc = _account_query_step(hstmt);
2225 account_s *account_record = NULL;
2227 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2230 while(rc == SQLITE_ROW) {
2231 account_record = (account_s*) malloc(sizeof(account_s));
2233 if (account_record == NULL) {
2234 ACCOUNT_FATAL("malloc Failed");
2238 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2239 _account_convert_column_to_account(hstmt, account_record);
2240 account_list = g_list_append(account_list, account_record);
2241 rc = _account_query_step(hstmt);
2245 _account_query_finalize(hstmt);
2250 for (iter = account_list; iter != NULL; iter = g_list_next(iter)) {
2251 account_s *account = NULL;
2252 account = (account_s*)iter->data;
2253 account_query_capability_by_account_id(_account_get_capability_text_cb, account->id, (void*)account);
2254 _account_query_custom_by_account_id(_account_get_custom_text_cb, account->id, (void*)account);
2255 callback((account_h)account, user_data);
2259 error_code = ACCOUNT_ERROR_NONE;
2262 if (hstmt != NULL) {
2263 _account_query_finalize(hstmt);
2267 _account_glist_free(account_list);
2268 account_list = NULL;
2274 int account_update_sync_status_by_id(int account_db_id, const account_sync_state_e sync_status)
2276 int error_code = ACCOUNT_ERROR_NONE;
2277 account_stmt hstmt = NULL;
2278 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2282 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2283 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2284 if ( (sync_status < 0) || (sync_status > ACCOUNT_SYNC_STATUS_RUNNING)) {
2285 ACCOUNT_ERROR("(%s)-(%d) sync_status is less than 0 or more than enum max.\n", __FUNCTION__, __LINE__);
2286 return ACCOUNT_ERROR_INVALID_PARAMETER;
2289 pthread_mutex_lock(&account_mutex);
2291 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2293 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_db_id);
2295 rc = _account_get_record_count(query);
2298 ACCOUNT_ERROR( "account_update_sync_status_by_id : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
2299 pthread_mutex_unlock(&account_mutex);
2300 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2303 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2305 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET sync_support=? WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
2306 hstmt = _account_prepare_query(query);
2308 _account_query_bind_int(hstmt, count, (int)sync_status);
2310 rc = _account_query_step(hstmt);
2312 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_DB_FAILED,
2313 ("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()));
2315 _account_query_finalize(hstmt);
2317 _account_insert_delete_update_notification_send(ACCOUNT_NOTI_NAME_SYNC_UPDATE);
2320 error_code = ACCOUNT_ERROR_NONE;
2323 if (hstmt != NULL) {
2324 _account_query_finalize(hstmt);
2328 pthread_mutex_unlock(&account_mutex);
2332 int account_query_account_by_account_id(int account_db_id, account_h *account)
2334 int error_code = ACCOUNT_ERROR_NONE;
2335 account_stmt hstmt = NULL;
2336 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2338 int ret = ACCOUNT_ERROR_NONE;
2340 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
2341 ACCOUNT_RETURN_VAL((*account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2342 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2344 ret = _do_account_owner_existance_check();
2345 if (ret != ACCOUNT_ERROR_NONE) {
2346 ACCOUNT_WARNING("_do_account_owner_existance_check Failed !!!\n");
2349 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2351 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
2352 hstmt = _account_prepare_query(query);
2354 rc = _account_query_step(hstmt);
2355 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2357 account_s *account_record = (account_s *)(*account);
2359 while (rc == SQLITE_ROW) {
2360 _account_convert_column_to_account(hstmt, account_record);
2361 ACCOUNT_VERBOSE("get account info by id %p\n", account_record);
2362 rc = _account_query_step(hstmt);
2365 _account_query_finalize(hstmt);
2366 account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
2367 _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record);
2370 error_code = ACCOUNT_ERROR_NONE;
2373 if (hstmt != NULL) {
2374 _account_query_finalize(hstmt);
2378 pthread_mutex_unlock(&account_mutex);
2382 int account_query_account_by_user_name(account_cb cb_func, const char *user_name, void *user_data)
2384 int error_code = ACCOUNT_ERROR_NONE;
2385 account_stmt hstmt = NULL;
2386 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2389 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("USER NAME IS NULL"));
2390 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
2393 int ret = ACCOUNT_ERROR_NONE;
2396 ret = _do_account_owner_existance_check();
2397 if (ret != ACCOUNT_ERROR_NONE) {
2398 ACCOUNT_WARNING("_do_account_owner_existance_check Failed !!!\n");
2401 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2403 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ?", ACCOUNT_TABLE);
2405 hstmt = _account_prepare_query(query);
2407 int binding_count = 1;
2408 _account_query_bind_text(hstmt, binding_count++, user_name);
2410 rc = _account_query_step(hstmt);
2412 account_s *account_head = NULL;
2414 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2418 account_head = (account_s*) malloc(sizeof(account_s));
2419 if (account_head == NULL) {
2420 ACCOUNT_FATAL("malloc Failed");
2421 if (hstmt != NULL) {
2422 _account_query_finalize(hstmt);
2425 return ACCOUNT_ERROR_OUT_OF_MEMORY;
2427 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2429 while (rc == SQLITE_ROW) {
2430 account_s* account_record = NULL;
2432 account_record = (account_s*) malloc(sizeof(account_s));
2434 if (account_record == NULL) {
2435 ACCOUNT_FATAL("malloc Failed");
2438 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2440 _account_convert_column_to_account(hstmt, account_record);
2442 account_head->account_list = g_list_append(account_head->account_list, account_record);
2444 rc = _account_query_step(hstmt);
2446 ACCOUNT_DEBUG("DETECTED COUNT %d\n", tmp);
2449 _account_query_finalize(hstmt);
2455 tmp = g_list_length(account_head->account_list);
2456 ACCOUNT_VERBOSE("GLIST LEN %d\n", tmp);
2458 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2460 account = (account_h)iter->data;
2462 account_s *testaccount = (account_s*)account;
2464 ACCOUNT_VERBOSE("id = %d", testaccount->id);
2465 ACCOUNT_VERBOSE("user_name = %s", testaccount->user_name);
2466 ACCOUNT_VERBOSE("email_address = %s", testaccount->email_address);
2467 ACCOUNT_VERBOSE("display_name = %s", testaccount->display_name);
2468 ACCOUNT_VERBOSE("icon_path = %s", testaccount->icon_path);
2469 ACCOUNT_VERBOSE("source = %s", testaccount->source);
2470 ACCOUNT_VERBOSE("package_name = %s", testaccount->package_name);
2471 ACCOUNT_VERBOSE("access_token = %s", testaccount->access_token);
2472 ACCOUNT_VERBOSE("domain_name = %s", testaccount->domain_name);
2473 ACCOUNT_VERBOSE("auth_type = %d", testaccount->auth_type);
2474 ACCOUNT_VERBOSE("secret = %d", testaccount->secret);
2475 ACCOUNT_VERBOSE("sync_support = %d", testaccount->sync_support);
2477 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
2478 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
2480 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
2482 cb_func(account, user_data);
2486 error_code = ACCOUNT_ERROR_NONE;
2489 if (hstmt != NULL) {
2490 _account_query_finalize(hstmt);
2494 if (account_head->account_list) {
2495 _account_glist_free(account_head->account_list);
2496 account_head->account_list = NULL;
2497 _account_free_account_items(account_head);
2499 _ACCOUNT_FREE(account_head);
2502 pthread_mutex_unlock(&account_mutex);
2506 int account_query_account_by_capability(account_cb cb_func, const char* capability_type, account_capability_state_e capability_value, void* user_data)
2508 int error_code = ACCOUNT_ERROR_NONE;
2509 account_stmt hstmt = NULL;
2510 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2512 int ret = ACCOUNT_ERROR_NONE;
2514 ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type IS NULL"));
2516 if ((capability_value < 0) || (capability_value > ACCOUNT_CAPABILITY_ENABLED)) {
2517 ACCOUNT_ERROR("(%s)-(%d) capability_value is not equal to 0 or 1.\n", __FUNCTION__, __LINE__);
2518 return ACCOUNT_ERROR_INVALID_PARAMETER;
2521 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
2522 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2524 ret = _do_account_owner_existance_check();
2525 if (ret != ACCOUNT_ERROR_NONE) {
2526 ACCOUNT_WARNING("_do_account_owner_existance_check Failed !!!\n");
2529 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2531 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id IN (SELECT account_id from %s WHERE key=? AND value=?)", ACCOUNT_TABLE, CAPABILITY_TABLE);
2533 hstmt = _account_prepare_query(query);
2535 int binding_count = 1;
2536 _account_query_bind_text(hstmt, binding_count++, (char*)capability_type);
2537 _account_query_bind_int(hstmt, binding_count++, (int)capability_value);
2539 rc = _account_query_step(hstmt);
2541 account_s* account_head = NULL;
2543 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2547 account_head = (account_s*) malloc(sizeof(account_s));
2548 if (account_head == NULL) {
2549 ACCOUNT_FATAL("malloc Failed");
2550 if (hstmt != NULL) {
2551 _account_query_finalize(hstmt);
2554 return ACCOUNT_ERROR_OUT_OF_MEMORY;
2556 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2558 while (rc == SQLITE_ROW) {
2559 account_s* account_record = NULL;
2561 account_record = (account_s*) malloc(sizeof(account_s));
2563 if (account_record == NULL) {
2564 ACCOUNT_FATAL("malloc Failed");
2567 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2569 _account_convert_column_to_account(hstmt, account_record);
2571 account_head->account_list = g_list_append(account_head->account_list, account_record);
2573 rc = _account_query_step(hstmt);
2575 ACCOUNT_VERBOSE("DETECTED COUNT %d\n", tmp);
2578 _account_query_finalize(hstmt);
2584 tmp = g_list_length(account_head->account_list);
2585 ACCOUNT_VERBOSE("GLIST LEN %d\n", tmp);
2587 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2588 account_h account = NULL;
2589 account = (account_h)iter->data;
2592 account_s* testaccount = (account_s*)account;
2594 ACCOUNT_VERBOSE("id = %d", testaccount->id);
2595 ACCOUNT_VERBOSE("user_name = %s", testaccount->user_name);
2596 ACCOUNT_VERBOSE("email_address = %s", testaccount->email_address);
2597 ACCOUNT_VERBOSE("display_name = %s", testaccount->display_name);
2598 ACCOUNT_VERBOSE("icon_path = %s", testaccount->icon_path);
2599 ACCOUNT_VERBOSE("source = %s", testaccount->source);
2600 ACCOUNT_VERBOSE("package_name = %s", testaccount->package_name);
2601 ACCOUNT_VERBOSE("access_token = %s", testaccount->access_token);
2602 ACCOUNT_VERBOSE("domain_name = %s", testaccount->domain_name);
2603 ACCOUNT_VERBOSE("auth_type = %d", testaccount->auth_type);
2604 ACCOUNT_VERBOSE("secret = %d", testaccount->secret);
2605 ACCOUNT_VERBOSE("sync_support = %d", testaccount->sync_support);
2607 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
2608 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
2610 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
2612 cb_func(account, user_data);
2617 error_code = ACCOUNT_ERROR_NONE;
2620 if (hstmt != NULL) {
2621 _account_query_finalize(hstmt);
2625 if (account_head->account_list) {
2626 _account_glist_free(account_head->account_list);
2627 account_head->account_list = NULL;
2628 _account_free_account_items(account_head);
2630 _ACCOUNT_FREE(account_head);
2633 pthread_mutex_unlock(&account_mutex);
2637 int account_query_account_by_package_name(account_cb cb_func, const char* package_name, void* user_data)
2639 int error_code = ACCOUNT_ERROR_NONE;
2640 account_stmt hstmt = NULL;
2641 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2643 int ret = ACCOUNT_ERROR_NONE;
2645 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("PACKAGE NAME IS NULL"));
2646 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("CALL BACK IS NULL"));
2647 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2649 ret = _do_account_owner_existance_check();
2650 if (ret != ACCOUNT_ERROR_NONE) {
2651 ACCOUNT_ERROR("_do_account_owner_existance_check Failed !!!\n");
2654 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2656 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE package_name=?", ACCOUNT_TABLE);
2658 hstmt = _account_prepare_query(query);
2660 int binding_count = 1;
2661 _account_query_bind_text(hstmt, binding_count++, package_name);
2663 rc = _account_query_step(hstmt);
2665 account_s* account_head = NULL;
2667 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2671 account_head = (account_s*) malloc(sizeof(account_s));
2672 if (account_head == NULL) {
2673 ACCOUNT_FATAL("malloc Failed");
2674 if (hstmt != NULL) {
2675 _account_query_finalize(hstmt);
2678 return ACCOUNT_ERROR_OUT_OF_MEMORY;
2680 ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2682 while (rc == SQLITE_ROW) {
2683 account_s* account_record = NULL;
2685 account_record = (account_s*) malloc(sizeof(account_s));
2687 if (account_record == NULL) {
2688 ACCOUNT_FATAL("malloc Failed");
2691 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2693 _account_convert_column_to_account(hstmt, account_record);
2695 account_head->account_list = g_list_append(account_head->account_list, account_record);
2697 rc = _account_query_step(hstmt);
2699 ACCOUNT_VERBOSE("DETECTED COUNT %d\n", tmp);
2702 _account_query_finalize(hstmt);
2707 tmp = g_list_length(account_head->account_list);
2708 ACCOUNT_DEBUG("GLIST LEN %d\n", tmp);
2710 for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2711 account_h account = NULL;
2712 account = (account_h)iter->data;
2714 account_s* testaccount = (account_s*)account;
2716 ACCOUNT_VERBOSE("id = %d", testaccount->id);
2717 ACCOUNT_VERBOSE("user_name = %s", testaccount->user_name);
2718 ACCOUNT_VERBOSE("email_address = %s", testaccount->email_address);
2719 ACCOUNT_VERBOSE("display_name = %s", testaccount->display_name);
2720 ACCOUNT_VERBOSE("icon_path = %s", testaccount->icon_path);
2721 ACCOUNT_VERBOSE("source = %s", testaccount->source);
2722 ACCOUNT_VERBOSE("package_name = %s", testaccount->package_name);
2723 ACCOUNT_VERBOSE("access_token = %s", testaccount->access_token);
2724 ACCOUNT_VERBOSE("domain_name = %s", testaccount->domain_name);
2725 ACCOUNT_VERBOSE("auth_type = %d", testaccount->auth_type);
2726 ACCOUNT_VERBOSE("secret = %d", testaccount->secret);
2727 ACCOUNT_VERBOSE("sync_support = %d", testaccount->sync_support);
2729 account_query_capability_by_account_id(_account_get_capability_text_cb, testaccount->id, (void*)testaccount);
2730 _account_query_custom_by_account_id(_account_get_custom_text_cb, testaccount->id, (void*)testaccount);
2732 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
2734 cb_func(account, user_data);
2738 error_code = ACCOUNT_ERROR_NONE;
2741 if (hstmt != NULL) {
2742 _account_query_finalize(hstmt);
2746 if (account_head->account_list) {
2747 _account_glist_free(account_head->account_list);
2748 account_head->account_list = NULL;
2749 _account_free_account_items(account_head);
2751 _ACCOUNT_FREE(account_head);
2754 pthread_mutex_unlock(&account_mutex);
2758 int account_delete(int account_id)
2760 int error_code = ACCOUNT_ERROR_NONE;
2761 account_stmt hstmt = NULL;
2762 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2763 int rc = 0, count = -1;
2764 int ret_transaction = 0;
2765 bool is_success = FALSE;
2767 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2769 /* Check requested ID to delete */
2770 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id=%d", ACCOUNT_TABLE, account_id);
2772 count = _account_get_record_count(query);
2774 ACCOUNT_ERROR("account id(%d) is not exist. count(%d)\n", account_id, count);
2775 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2778 /* transaction control required*/
2779 ret_transaction = _account_begin_transaction();
2781 if (ret_transaction != ACCOUNT_ERROR_NONE) {
2782 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
2783 pthread_mutex_unlock(&account_mutex);
2784 return ret_transaction;
2787 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2788 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2790 hstmt = _account_prepare_query(query);
2791 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
2792 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
2794 rc = _account_query_step(hstmt);
2795 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2797 _account_query_finalize(hstmt);
2800 ACCOUNT_MEMSET(query, 0, sizeof(query));
2802 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2804 hstmt = _account_prepare_query(query);
2805 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
2806 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
2808 rc = _account_query_step(hstmt);
2809 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
2811 _account_query_finalize(hstmt);
2814 /* delete custom data */
2815 ACCOUNT_MEMSET(query, 0, sizeof(query));
2817 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
2819 hstmt = _account_prepare_query(query);
2820 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
2821 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
2823 rc = _account_query_step(hstmt);
2824 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. id=%d, rc=%d\n", account_id, rc));
2826 _account_query_finalize(hstmt);
2831 if (hstmt != NULL) {
2832 _account_query_finalize(hstmt);
2836 ret_transaction = _account_end_transaction(is_success);
2838 if (ret_transaction != ACCOUNT_ERROR_NONE) {
2839 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
2841 if (is_success == true)
2842 _account_insert_delete_update_notification_send(ACCOUNT_NOTI_NAME_DELETE);
2845 pthread_mutex_unlock(&account_mutex);
2851 int account_delete_from_db_by_id(int account_db_id)
2853 ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID IS LESS THAN ZERO."));
2855 return account_delete(account_db_id);
2858 static int _account_query_account_by_username_and_package(const char* username, const char* package_name, account_h *account)
2860 int error_code = ACCOUNT_ERROR_NONE;
2861 account_stmt hstmt = NULL;
2862 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2864 int ret = ACCOUNT_ERROR_NONE;
2865 int binding_count = 1;
2867 ACCOUNT_RETURN_VAL((username != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("username IS NULL"));
2868 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name IS NULL"));
2869 ACCOUNT_RETURN_VAL((*account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT IS NULL"));
2870 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2872 ret = _do_account_owner_existance_check();
2873 if (ret != ACCOUNT_ERROR_NONE) {
2874 ACCOUNT_WARNING("_do_account_owner_existance_check Failed !!!\n");
2877 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2879 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
2880 hstmt = _account_prepare_query(query);
2882 _account_query_bind_text(hstmt, binding_count++, username);
2883 _account_query_bind_text(hstmt, binding_count++, package_name);
2885 rc = _account_query_step(hstmt);
2886 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2888 account_s *account_record = (account_s *)(*account);
2890 while (rc == SQLITE_ROW) {
2891 _account_convert_column_to_account(hstmt, account_record);
2892 ACCOUNT_VERBOSE("get account info by id %p\n", account_record);
2893 rc = _account_query_step(hstmt);
2896 _account_query_finalize(hstmt);
2897 account_query_capability_by_account_id(_account_get_capability_text_cb, account_record->id, (void*)account_record);
2898 _account_query_custom_by_account_id(_account_get_custom_text_cb, account_record->id, (void*)account_record);
2901 error_code = ACCOUNT_ERROR_NONE;
2904 if (hstmt != NULL) {
2905 _account_query_finalize(hstmt);
2909 pthread_mutex_unlock(&account_mutex);
2913 int account_delete_from_db_by_user_name(char *user_name, char *package_name)
2915 int error_code = ACCOUNT_ERROR_NONE;
2916 account_stmt hstmt = NULL;
2917 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
2919 int ret_transaction = 0;
2920 bool is_success = FALSE;
2921 account_h account = NULL;
2922 int binding_count = 1;
2923 int account_id = -1;
2925 ACCOUNT_RETURN_VAL((user_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("user_name is null!"));
2926 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
2927 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2929 rc = account_create(&account);
2930 rc = _account_query_account_by_username_and_package(user_name, package_name, &account);
2932 rc = account_get_account_id(account, &account_id);
2934 rc = account_destroy(account);
2936 /* transaction control required*/
2937 ret_transaction = _account_begin_transaction();
2939 if (ret_transaction != ACCOUNT_ERROR_NONE) {
2940 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
2941 pthread_mutex_unlock(&account_mutex);
2942 return ret_transaction;
2945 /* delete custom data */
2946 ACCOUNT_MEMSET(query, 0, sizeof(query));
2947 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = ?", ACCOUNT_CUSTOM_TABLE);
2949 hstmt = _account_prepare_query(query);
2951 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
2952 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
2954 _account_query_bind_int(hstmt, binding_count++, account_id);
2956 rc = _account_query_step(hstmt);
2957 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2959 _account_query_finalize(hstmt);
2962 /* delete capability */
2963 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", CAPABILITY_TABLE);
2965 hstmt = _account_prepare_query(query);
2967 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
2968 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
2971 _account_query_bind_text(hstmt, binding_count++, user_name);
2972 _account_query_bind_text(hstmt, binding_count++, package_name);
2974 rc = _account_query_step(hstmt);
2975 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2977 _account_query_finalize(hstmt);
2980 ACCOUNT_MEMSET(query, 0, sizeof(query));
2982 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
2984 hstmt = _account_prepare_query(query);
2985 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
2986 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
2989 _account_query_bind_text(hstmt, binding_count++, user_name);
2990 _account_query_bind_text(hstmt, binding_count++, package_name);
2992 rc = _account_query_step(hstmt);
2993 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));
2995 _account_query_finalize(hstmt);
3001 if (hstmt != NULL) {
3002 _account_query_finalize(hstmt);
3006 ret_transaction = _account_end_transaction(is_success);
3008 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3009 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3011 if (is_success == true)
3012 _account_insert_delete_update_notification_send(ACCOUNT_NOTI_NAME_DELETE);
3015 pthread_mutex_unlock(&account_mutex);
3020 int account_delete_from_db_by_package_name(char *package_name)
3022 int error_code = ACCOUNT_ERROR_NONE;
3023 account_stmt hstmt = NULL;
3024 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3026 int ret_transaction = 0;
3027 bool is_success = FALSE;
3028 int binding_count = 1;
3030 ACCOUNT_RETURN_VAL((package_name != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("package_name is null!"));
3031 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3033 /* transaction control required*/
3034 ret_transaction = _account_begin_transaction();
3036 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3037 ACCOUNT_ERROR("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3038 pthread_mutex_unlock(&account_mutex);
3039 return ret_transaction;
3042 /* delete custom table */
3043 ACCOUNT_MEMSET(query, 0, sizeof(query));
3044 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", ACCOUNT_CUSTOM_TABLE);
3046 hstmt = _account_prepare_query(query);
3048 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3049 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3052 _account_query_bind_text(hstmt, binding_count++, package_name);
3054 rc = _account_query_step(hstmt);
3055 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3057 _account_query_finalize(hstmt);
3060 /* delete capability table */
3061 ACCOUNT_MEMSET(query, 0, sizeof(query));
3062 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", CAPABILITY_TABLE);
3064 hstmt = _account_prepare_query(query);
3066 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3067 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3070 _account_query_bind_text(hstmt, binding_count++, package_name);
3072 rc = _account_query_step(hstmt);
3073 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3075 _account_query_finalize(hstmt);
3078 /* delete account table */
3079 ACCOUNT_MEMSET(query, 0, sizeof(query));
3081 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", ACCOUNT_TABLE);
3083 hstmt = _account_prepare_query(query);
3084 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3085 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3088 _account_query_bind_text(hstmt, binding_count++, package_name);
3090 rc = _account_query_step(hstmt);
3091 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));
3093 _account_query_finalize(hstmt);
3099 if (hstmt != NULL) {
3100 _account_query_finalize(hstmt);
3104 ret_transaction = _account_end_transaction(is_success);
3106 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3107 ACCOUNT_ERROR("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3109 if (is_success == true)
3110 _account_insert_delete_update_notification_send(ACCOUNT_NOTI_NAME_DELETE);
3113 pthread_mutex_unlock(&account_mutex);
3118 int account_get_total_count_from_db(int *count)
3121 ACCOUNT_ERROR("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__);
3122 return ACCOUNT_ERROR_INVALID_PARAMETER;
3124 int ret = ACCOUNT_ERROR_NONE;
3125 char query[1024] = {0, };
3126 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3127 ACCOUNT_SNPRINTF(query, sizeof(query), "select count(*) from %s", ACCOUNT_TABLE);
3129 ret = _do_account_owner_existance_check();
3130 if (ret != ACCOUNT_ERROR_NONE) {
3131 ACCOUNT_WARNING("_do_account_owner_existance_check Failed !!!\n");
3134 *count = _account_get_record_count(query);
3137 account_stmt pStmt = NULL;
3139 assert(NULL != g_hAccountDB);
3140 rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
3142 rc = sqlite3_step(pStmt);
3143 if (SQLITE_ROW != rc) {
3144 ACCOUNT_ERROR("[ERROR] sqlite3_step() failed\n");
3145 sqlite3_finalize(pStmt);
3146 return ACCOUNT_ERROR_DB_FAILED;
3149 ncount = sqlite3_column_int(pStmt, 0);
3153 ACCOUNT_VERBOSE("Number of account : %d", ncount);
3154 sqlite3_finalize(pStmt);
3157 ACCOUNT_ERROR("[ERROR] Number of account : %d, End", ncount);
3158 return ACCOUNT_ERROR_DB_FAILED;
3161 return ACCOUNT_ERROR_NONE;
3164 int account_destroy(account_h account)
3166 account_s *data = (account_s*)account;
3168 ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!"));
3170 ACCOUNT_INFO("destroy handle=%p\n", data);
3172 _account_free_account_items(data);
3173 _ACCOUNT_FREE(data);
3175 return ACCOUNT_ERROR_NONE;
3178 static int _account_type_free_label_items(label_s *data)
3180 _ACCOUNT_FREE(data->app_id);
3181 _ACCOUNT_FREE(data->label);
3182 _ACCOUNT_FREE(data->locale);
3184 return ACCOUNT_ERROR_NONE;
3187 static int _account_type_gslist_free(GSList* list)
3189 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL"));
3193 for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
3194 label_s *label_data = (label_s*)iter->data;
3195 _account_type_free_label_items(label_data);
3196 _ACCOUNT_FREE(label_data);
3202 return ACCOUNT_ERROR_NONE;
3205 static int _account_type_item_free(account_type_s *data)
3207 _ACCOUNT_FREE(data->app_id);
3208 _ACCOUNT_FREE(data->service_provider_id);
3209 _ACCOUNT_FREE(data->icon_path);
3210 _ACCOUNT_FREE(data->small_icon_path);
3212 return ACCOUNT_ERROR_NONE;
3215 static int _account_type_glist_free(GList* list)
3217 ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Glist is NULL"));
3221 for (iter = list; iter != NULL; iter = g_list_next(iter)) {
3222 account_type_s *account_type_record = (account_type_s*)iter->data;
3223 _account_type_item_free(account_type_record);
3224 _ACCOUNT_FREE(account_type_record);
3230 return ACCOUNT_ERROR_NONE;
3233 static int _account_type_free_account_type_items(account_type_s *data)
3235 _account_type_item_free(data);
3237 _account_type_gslist_free(data->label_list);
3238 _account_type_glist_free(data->account_type_list);
3240 return ACCOUNT_ERROR_NONE;
3243 int account_type_create(account_type_h *account_type)
3245 if (!account_type) {
3246 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
3247 return ACCOUNT_ERROR_INVALID_PARAMETER;
3250 account_type_s *data = (account_type_s*)malloc(sizeof(account_type_s));
3253 ACCOUNT_ERROR("Memory Allocation Failed");
3254 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3257 ACCOUNT_MEMSET(data, 0, sizeof(account_type_s));
3259 ACCOUNT_VERBOSE("create handle=%p\n", *account_type);
3261 *account_type = (account_type_h)data;
3263 return ACCOUNT_ERROR_NONE;
3266 int account_type_destroy(account_type_h account_type)
3268 account_type_s *data = (account_type_s*)account_type;
3270 ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account type handle is null!"));
3272 ACCOUNT_VERBOSE("destroy handle=%p\n", data);
3274 _account_type_free_account_type_items(data);
3275 _ACCOUNT_FREE(data);
3277 return ACCOUNT_ERROR_NONE;
3280 //app_id mandatory field
3281 int account_type_set_app_id(account_type_h account_type, const char *app_id)
3283 if (!account_type) {
3284 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3285 return ACCOUNT_ERROR_INVALID_PARAMETER;
3289 ACCOUNT_ERROR("(%s)-(%d) app_id is NULL.\n", __FUNCTION__, __LINE__);
3290 return ACCOUNT_ERROR_INVALID_PARAMETER;
3293 account_type_s *data = (account_type_s*)account_type;
3295 _ACCOUNT_FREE(data->app_id);
3296 data->app_id = _account_get_text(app_id);
3298 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->app_id, app_id);
3299 return ACCOUNT_ERROR_NONE;
3302 //service_provider_id mandatory field
3303 int account_type_set_service_provider_id(account_type_h account_type, const char *service_provider_id)
3305 if (!account_type) {
3306 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3307 return ACCOUNT_ERROR_INVALID_PARAMETER;
3310 if (!service_provider_id) {
3311 ACCOUNT_ERROR("(%s)-(%d) service_provider_id is NULL.\n", __FUNCTION__, __LINE__);
3312 return ACCOUNT_ERROR_INVALID_PARAMETER;
3315 account_type_s *data = (account_type_s*)account_type;
3317 _ACCOUNT_FREE(data->service_provider_id);
3318 data->service_provider_id = _account_get_text(service_provider_id);
3320 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->service_provider_id, service_provider_id);
3321 return ACCOUNT_ERROR_NONE;
3324 int account_type_set_icon_path(account_type_h account_type, const char *icon_path)
3326 if (!account_type) {
3327 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3328 return ACCOUNT_ERROR_INVALID_PARAMETER;
3332 ACCOUNT_ERROR("(%s)-(%d) icon_path is NULL.\n", __FUNCTION__, __LINE__);
3333 return ACCOUNT_ERROR_INVALID_PARAMETER;
3336 account_type_s *data = (account_type_s*)account_type;
3338 _ACCOUNT_FREE(data->icon_path);
3339 data->icon_path = _account_get_text(icon_path);
3341 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->icon_path, icon_path);
3342 return ACCOUNT_ERROR_NONE;
3345 int account_type_set_small_icon_path(account_type_h account_type, const char *small_icon_path)
3347 if (!account_type) {
3348 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3349 return ACCOUNT_ERROR_INVALID_PARAMETER;
3352 if (!small_icon_path) {
3353 ACCOUNT_ERROR("(%s)-(%d) small_icon_path is NULL.\n", __FUNCTION__, __LINE__);
3354 return ACCOUNT_ERROR_INVALID_PARAMETER;
3357 account_type_s *data = (account_type_s*)account_type;
3359 _ACCOUNT_FREE(data->small_icon_path);
3360 data->small_icon_path = _account_get_text(small_icon_path);
3362 ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->small_icon_path, small_icon_path);
3363 return ACCOUNT_ERROR_NONE;
3366 int account_type_set_multiple_account_support(account_type_h account_type, const bool multiple_account_support)
3368 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__));
3370 account_type_s *data = (account_type_s*)account_type;
3372 data->multiple_account_support = multiple_account_support;
3374 return ACCOUNT_ERROR_NONE;
3378 int account_type_set_label(account_type_h account_type, const char* label, const char* locale)
3380 if (!account_type) {
3381 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3382 return ACCOUNT_ERROR_INVALID_PARAMETER;
3385 if(!label || !locale) {
3386 ACCOUNT_ERROR("(%s)-(%d) label(%p) or locale(%p) is NULL.\n", __FUNCTION__, __LINE__, label, locale);
3387 return ACCOUNT_ERROR_INVALID_PARAMETER;
3390 account_type_s *data = (account_type_s*)account_type;
3391 label_s *label_data = (label_s*)malloc(sizeof(label_s));
3393 if (label_data == NULL) {
3394 ACCOUNT_FATAL("(%s)-(%d) Malloc fail. label_data is NULL.\n", __FUNCTION__, __LINE__);
3395 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3397 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
3399 label_data->label = _account_get_text(label);
3400 label_data->locale = _account_get_text(locale);
3402 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
3404 return ACCOUNT_ERROR_NONE;
3408 int account_type_get_app_id(account_type_h account_type, char **app_id)
3410 if (!account_type) {
3411 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
3412 return ACCOUNT_ERROR_INVALID_PARAMETER;
3416 ACCOUNT_ERROR("(%s)-(%d) app id is NULL.\n", __FUNCTION__, __LINE__);
3417 return ACCOUNT_ERROR_INVALID_PARAMETER;
3420 account_type_s *data = (account_type_s*)account_type;
3423 *app_id = _account_get_text(data->app_id);
3425 return ACCOUNT_ERROR_NONE;
3428 int account_type_get_service_provider_id(account_type_h account_type, char **service_provider_id)
3430 if (!account_type) {
3431 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
3432 return ACCOUNT_ERROR_INVALID_PARAMETER;
3435 if (!service_provider_id) {
3436 ACCOUNT_ERROR("(%s)-(%d) service provider id is NULL.\n", __FUNCTION__, __LINE__);
3437 return ACCOUNT_ERROR_INVALID_PARAMETER;
3440 account_type_s *data = (account_type_s*)account_type;
3442 (*service_provider_id) = NULL;
3443 *service_provider_id = _account_get_text(data->service_provider_id);
3445 return ACCOUNT_ERROR_NONE;
3448 int account_type_get_icon_path(account_type_h account_type, char **icon_path)
3450 if (!account_type) {
3451 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
3452 return ACCOUNT_ERROR_INVALID_PARAMETER;
3456 ACCOUNT_ERROR("(%s)-(%d) icon path is NULL.\n", __FUNCTION__, __LINE__);
3457 return ACCOUNT_ERROR_INVALID_PARAMETER;
3460 account_type_s *data = (account_type_s*)account_type;
3462 (*icon_path) = NULL;
3463 *icon_path = _account_get_text(data->icon_path);
3465 return ACCOUNT_ERROR_NONE;
3468 int account_type_get_small_icon_path(account_type_h account_type, char **small_icon_path)
3470 if (!account_type) {
3471 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
3472 return ACCOUNT_ERROR_INVALID_PARAMETER;
3475 if (!small_icon_path) {
3476 ACCOUNT_ERROR("(%s)-(%d) icon path is NULL.\n", __FUNCTION__, __LINE__);
3477 return ACCOUNT_ERROR_INVALID_PARAMETER;
3480 account_type_s *data = (account_type_s*)account_type;
3482 (*small_icon_path) = NULL;
3483 *small_icon_path = _account_get_text(data->small_icon_path);
3485 return ACCOUNT_ERROR_NONE;
3488 int account_type_get_multiple_account_support(account_type_h account_type, int *multiple_account_support)
3490 if (!account_type) {
3491 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3492 return ACCOUNT_ERROR_INVALID_PARAMETER;
3494 if (!multiple_account_support) {
3495 ACCOUNT_ERROR("(%s)-(%d) multiple_account_support is NULL.\n", __FUNCTION__, __LINE__);
3496 return ACCOUNT_ERROR_INVALID_PARAMETER;
3499 account_type_s *data = (account_type_s*)account_type;
3501 *multiple_account_support = data->multiple_account_support;
3503 return ACCOUNT_ERROR_NONE;
3506 int account_type_get_label(account_type_h account_type, account_label_cb cb_func, void *user_data)
3508 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
3509 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3512 account_type_s *data = (account_type_s*)account_type;
3514 for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) {
3515 label_s *label_data = NULL;
3517 label_data = (label_s*)iter->data;
3519 ACCOUNT_VERBOSE("account_type_get_label :: app_id=%s, label=%s, locale=%s", label_data->app_id, label_data->label, label_data->locale);
3521 cb_func(label_data->app_id, label_data->label, label_data->locale, user_data);
3524 return ACCOUNT_ERROR_NONE;
3527 static gboolean _account_type_check_duplicated(account_type_s *data)
3529 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3532 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3534 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId='%s'"
3535 , ACCOUNT_TYPE_TABLE, data->app_id);
3537 count = _account_get_record_count(query);
3539 ACCOUNT_VERBOSE("_account_check_duplicated : duplicated %d account(s) exist!, user_name=%s, domain_name=%s\n",
3540 count, data->app_id, data->service_provider_id);
3547 static int _account_type_convert_account_to_sql(account_type_s *account_type, account_stmt hstmt, char *sql_value)
3551 /*Caution : Keep insert query orders.*/
3554 _account_query_bind_text(hstmt, count++, (char*)account_type->app_id);
3556 /* 2. service provider id*/
3557 _account_query_bind_text(hstmt, count++, (char*)account_type->service_provider_id);
3560 _account_query_bind_text(hstmt, count++, (char*)account_type->icon_path);
3562 /* 4. small icon path*/
3563 _account_query_bind_text(hstmt, count++, (char*)account_type->small_icon_path);
3565 /* 5. multiple accont support*/
3566 _account_query_bind_int(hstmt, count++, account_type->multiple_account_support);
3572 static int _account_type_execute_insert_query(account_type_s *account_type)
3575 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3576 int error_code = ACCOUNT_ERROR_NONE;
3577 account_stmt hstmt = NULL;
3579 /* check mandatory field */
3580 // app id & service provider id
3581 if (!account_type->app_id) {
3582 ACCOUNT_ERROR("App name is mandetory field, it can not be NULL!!!!\n");
3583 return ACCOUNT_ERROR_INVALID_PARAMETER;
3586 if (!account_type->service_provider_id) {
3587 ACCOUNT_ERROR("service_provider_id is mandetory field, it can not be NULL!!!!\n");
3588 return ACCOUNT_ERROR_INVALID_PARAMETER;
3591 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3592 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s( AppId, ServiceProviderId , IconPath , SmallIconPath , MultipleAccountSupport ) values "
3593 "(?, ?, ?, ?, ?)", ACCOUNT_TYPE_TABLE);
3595 hstmt = _account_prepare_query(query);
3596 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
3598 _account_type_convert_account_to_sql(account_type, hstmt, query);
3600 rc = _account_query_step(hstmt);
3601 if (rc != SQLITE_DONE) {
3602 ACCOUNT_FATAL( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
3603 error_code = ACCOUNT_ERROR_DB_FAILED;
3606 _account_query_finalize(hstmt);
3612 static int _account_type_insert_label(account_type_s *account_type)
3615 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3616 account_stmt hstmt = NULL;
3618 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
3620 if (g_slist_length( account_type->label_list)==0) {
3621 ACCOUNT_ERROR( "_account_type_insert_label, no label\n");
3622 return ACCOUNT_ERROR_NONE;
3625 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId = '%s'", ACCOUNT_TYPE_TABLE, account_type->app_id);
3627 rc = _account_get_record_count(query);
3630 ACCOUNT_ERROR( "_account_insert_label : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
3631 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3637 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
3640 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3641 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
3642 "(?, ?, ?) ", LABEL_TABLE);
3644 hstmt = _account_prepare_query(query);
3646 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
3648 label_s* label_data = NULL;
3649 label_data = (label_s*)iter->data;
3650 ACCOUNT_VERBOSE( "label_data->appid = %s, label_data->label = %s, label_data->locale \n", label_data->app_id, label_data->label, label_data->locale);
3652 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
3653 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3654 ret = _account_query_bind_text(hstmt, count++, label_data->label);
3655 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3656 ret = _account_query_bind_text(hstmt, count++, (char*)label_data->locale);
3657 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3659 rc = _account_query_step(hstmt);
3661 if (rc != SQLITE_DONE) {
3662 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
3666 _account_query_finalize(hstmt);
3671 ACCOUNT_VERBOSE( "_account_type_insert_label() DONE\n");
3673 return ACCOUNT_ERROR_NONE;
3676 int account_type_insert_to_db(account_type_h account_type, int* account_type_id)
3678 int error_code = ACCOUNT_ERROR_NONE;
3680 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3681 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE HANDLE IS NULL"));
3682 ACCOUNT_RETURN_VAL((account_type_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE ID POINTER IS NULL"));
3684 account_type_s *data = (account_type_s*)account_type;
3686 pthread_mutex_lock(&account_mutex);
3688 if (_account_type_check_duplicated(data)) {
3689 error_code = ACCOUNT_ERROR_DUPLICATED;
3691 *account_type_id = _account_get_next_sequence(ACCOUNT_TYPE_TABLE);
3693 error_code = _account_type_execute_insert_query(data);
3695 if (error_code != ACCOUNT_ERROR_NONE)
3696 *account_type_id = -1;
3699 ACCOUNT_INFO( "_account_type_execute_insert_query, insert error_code : %d", error_code);
3701 _account_type_insert_label(data);
3703 pthread_mutex_unlock(&account_mutex);
3705 return ACCOUNT_ERROR_NONE;
3708 static int _account_type_update_label(account_type_s *account_type, const char* app_id)
3711 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3712 account_stmt hstmt = NULL;
3714 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
3716 if (g_slist_length( account_type->label_list)==0) {
3717 ACCOUNT_ERROR( "_account_type_update_label, no label\n");
3718 return ACCOUNT_ERROR_NONE;
3721 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3723 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId=? ", LABEL_TABLE);
3724 hstmt = _account_prepare_query(query);
3726 _account_query_bind_text(hstmt, count++, app_id);
3727 rc = _account_query_step(hstmt);
3729 if (rc != SQLITE_DONE) {
3730 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
3731 return ACCOUNT_ERROR_DB_FAILED;
3733 _account_query_finalize(hstmt);
3738 for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
3741 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3742 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
3743 "(?, ?, ?) ", LABEL_TABLE);
3745 hstmt = _account_prepare_query(query);
3747 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
3749 label_s* label_data = NULL;
3750 label_data = (label_s*)iter->data;
3752 ret = _account_query_bind_text(hstmt, count++, account_type->app_id);
3753 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3754 ret = _account_query_bind_text(hstmt, count++, label_data->label);
3755 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3756 ret = _account_query_bind_text(hstmt, count++, label_data->locale);
3757 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
3759 rc = _account_query_step(hstmt);
3761 if (rc != SQLITE_DONE) {
3762 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
3765 _account_query_finalize(hstmt);
3769 ACCOUNT_VERBOSE( "_account_type_update_label() DONE\n");
3771 return ACCOUNT_ERROR_NONE;
3775 static int _account_type_update_account(account_type_s *account_type, const char* app_id)
3777 int rc = 0, binding_count =1;
3778 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3779 int error_code = ACCOUNT_ERROR_NONE;
3780 account_stmt hstmt = NULL;
3782 if (!account_type->app_id) {
3783 ACCOUNT_ERROR("app id is mandetory field, it can not be NULL!!!!\n");
3784 return ACCOUNT_ERROR_INVALID_PARAMETER;
3787 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3788 ACCOUNT_SNPRINTF(query, sizeof(query), "UPDATE %s SET AppId=?, ServiceProviderId=?, IconPath=?, "
3789 "SmallIconPath=?, MultipleAccountSupport=? WHERE AppId=? ", ACCOUNT_TYPE_TABLE);
3791 hstmt = _account_prepare_query(query);
3792 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_svc_query_prepare() failed(%s).\n", _account_db_err_msg()));
3794 binding_count = _account_type_convert_account_to_sql(account_type, hstmt, query);
3795 _account_query_bind_text(hstmt, binding_count++, app_id);
3797 rc = _account_query_step(hstmt);
3798 if (rc != SQLITE_DONE) {
3799 ACCOUNT_FATAL( "account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg());
3802 _account_query_finalize(hstmt);
3806 error_code = _account_type_update_label(account_type, app_id);
3811 int account_type_update_to_db_by_app_id(account_type_h account_type, const char* app_id)
3813 ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("DATA IS NULL"));
3814 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3815 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3817 int error_code = ACCOUNT_ERROR_NONE;
3818 account_type_s* data = (account_type_s*)account_type;
3820 pthread_mutex_lock(&account_mutex);
3822 error_code = _account_type_update_account(data, app_id);
3824 pthread_mutex_unlock(&account_mutex);
3826 return ACCOUNT_ERROR_NONE;
3829 int account_type_delete_by_app_id(const char* app_id)
3831 int error_code = ACCOUNT_ERROR_NONE;
3832 account_stmt hstmt = NULL;
3833 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3834 int rc = 0, count = -1;
3835 int ret_transaction = 0;
3836 int binding_count = 1;
3837 bool is_success = FALSE;
3839 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3840 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("The database isn't connected."));
3842 /* Check requested ID to delete */
3843 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
3845 count = _account_get_record_count(query);
3847 ACCOUNT_ERROR("app id(%s) is not exist. count(%d)\n", app_id, count);
3848 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3851 /* transaction control required*/
3852 ret_transaction = _account_begin_transaction();
3854 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3855 ACCOUNT_FATAL("account_delete:_account_begin_transaction fail %d\n", ret_transaction);
3856 pthread_mutex_unlock(&account_mutex);
3857 return ret_transaction;
3860 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", LABEL_TABLE);
3862 hstmt = _account_prepare_query(query);
3863 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3864 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3866 _account_query_bind_text(hstmt, binding_count++, app_id);
3868 rc = _account_query_step(hstmt);
3869 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3871 _account_query_finalize(hstmt);
3875 ACCOUNT_MEMSET(query, 0, sizeof(query));
3877 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ? ", ACCOUNT_TYPE_TABLE);
3879 hstmt = _account_prepare_query(query);
3880 ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3881 ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3883 _account_query_bind_text(hstmt, binding_count++, app_id);
3885 rc = _account_query_step(hstmt);
3886 ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found. AppId=%s, rc=%d\n", app_id, rc));
3888 _account_query_finalize(hstmt);
3894 if (hstmt != NULL) {
3895 _account_query_finalize(hstmt);
3899 ret_transaction = _account_end_transaction(is_success);
3901 if (ret_transaction != ACCOUNT_ERROR_NONE) {
3902 ACCOUNT_FATAL("account_svc_delete:_account_svc_end_transaction fail %d, is_success=%d\n", ret_transaction, is_success);
3905 pthread_mutex_unlock(&account_mutex);
3910 static void _account_type_convert_column_to_account_type(account_stmt hstmt, account_type_s *account_type_record)
3912 char *textbuf = NULL;
3914 account_type_record->id = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_ID);
3916 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_APP_ID);
3917 _account_db_data_to_text(textbuf, &(account_type_record->app_id));
3919 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SERVICE_PROVIDER_ID);
3920 _account_db_data_to_text(textbuf, &(account_type_record->service_provider_id));
3922 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_ICON_PATH);
3923 _account_db_data_to_text(textbuf, &(account_type_record->icon_path));
3925 textbuf = _account_query_table_column_text(hstmt, ACCOUNT_TYPE_FIELD_SMALL_ICON_PATH);
3926 _account_db_data_to_text(textbuf, &(account_type_record->small_icon_path));
3928 account_type_record->multiple_account_support = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_MULTIPLE_ACCOUNT_SUPPORT);
3930 ACCOUNT_VERBOSE("END _account_type_convert_column_to_account_type");
3933 static void _account_type_convert_column_to_label(account_stmt hstmt, label_s *label_record)
3935 char *textbuf = NULL;
3937 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_APP_ID);
3938 _account_db_data_to_text(textbuf, &(label_record->app_id));
3940 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LABEL);
3941 _account_db_data_to_text(textbuf, &(label_record->label));
3943 textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LOCALE);
3944 _account_db_data_to_text(textbuf, &(label_record->locale));
3946 ACCOUNT_VERBOSE("END _account_type_convert_column_to_label");
3949 int account_type_query_label_by_app_id(account_label_cb cb_func, const char* app_id, void *user_data )
3951 int error_code = ACCOUNT_ERROR_NONE;
3952 account_stmt hstmt = NULL;
3953 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3954 int rc = 0, binding_count = 1;
3956 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
3957 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
3958 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
3960 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3962 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3963 hstmt = _account_prepare_query(query);
3965 _account_query_bind_text(hstmt, binding_count++, app_id);
3967 rc = _account_query_step(hstmt);
3968 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3970 label_s* label_record = NULL;
3972 while (rc == SQLITE_ROW) {
3973 label_record = (label_s*) malloc(sizeof(label_s));
3975 if (label_record == NULL) {
3976 ACCOUNT_FATAL("malloc Failed");
3980 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3982 _account_type_convert_column_to_label(hstmt, label_record);
3984 cb_func(label_record->app_id, label_record->label , label_record->locale, user_data);
3986 _account_type_free_label_items(label_record);
3987 _ACCOUNT_FREE(label_record);
3989 rc = _account_query_step(hstmt);
3992 _account_query_finalize(hstmt);
3995 error_code = ACCOUNT_ERROR_NONE;
3998 if (hstmt != NULL) {
3999 _account_query_finalize(hstmt);
4003 pthread_mutex_unlock(&account_mutex);
4007 int _account_type_label_get_app_id(label_h label, char **app_id)
4010 ACCOUNT_ERROR("(%s)-(%d) label handle is NULL.\n", __FUNCTION__, __LINE__);
4011 return ACCOUNT_ERROR_INVALID_PARAMETER;
4015 ACCOUNT_ERROR("(%s)-(%d) app_id is NULL.\n", __FUNCTION__, __LINE__);
4016 return ACCOUNT_ERROR_INVALID_PARAMETER;
4019 label_s *data = (label_s*)label;
4023 *app_id = _account_get_text(data->app_id);
4025 return ACCOUNT_ERROR_NONE;
4028 bool _account_get_label_text_cb(char* app_id, char* label, char* locale, void *user_data)
4030 account_type_s *data = (account_type_s*)user_data;
4032 label_s *label_data = (label_s*)malloc(sizeof(label_s));
4034 if (label_data == NULL) {
4035 ACCOUNT_FATAL("_account_get_label_text_cb : MALLOC FAIL\n");
4038 ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
4040 label_data->app_id = _account_get_text(app_id);
4041 label_data->label = _account_get_text(label);
4042 label_data->locale = _account_get_text(locale);
4044 data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
4046 ACCOUNT_VERBOSE("_account_get_label_text_cb :: appid=%s, label=%s\n", label_data->app_id, label_data->label);
4052 int account_type_query_by_app_id(const char* app_id, account_type_h *account_type)
4054 int error_code = ACCOUNT_ERROR_NONE;
4055 account_stmt hstmt = NULL;
4056 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4057 int rc = 0, binding_count = 1;
4059 ACCOUNT_RETURN_VAL((app_id != 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("APP ID IS NULL"));
4060 ACCOUNT_RETURN_VAL((*account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT TYPE IS NULL"));
4061 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4063 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4065 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
4066 hstmt = _account_prepare_query(query);
4068 _account_query_bind_text(hstmt, binding_count++, app_id);
4070 rc = _account_query_step(hstmt);
4071 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4073 account_type_s *account_type_record = (account_type_s *)(*account_type);
4075 while (rc == SQLITE_ROW) {
4076 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4077 ACCOUNT_DEBUG("get account info by id %p\n", account_type_record);
4078 rc = _account_query_step(hstmt);
4081 _account_query_finalize(hstmt);
4082 account_type_query_label_by_app_id(_account_get_label_text_cb, account_type_record->app_id, (void*)account_type_record);
4085 error_code = ACCOUNT_ERROR_NONE;
4088 if (hstmt != NULL) {
4089 _account_query_finalize(hstmt);
4093 pthread_mutex_unlock(&account_mutex);
4097 int account_type_foreach_account_type_from_db(account_type_cb callback, void *user_data)
4099 int error_code = ACCOUNT_ERROR_NONE;
4100 account_stmt hstmt = NULL;
4101 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4103 GList *account_type_list = NULL;
4105 ACCOUNT_RETURN_VAL((callback != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INFO IS NULL"));
4106 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4108 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4110 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
4111 hstmt = _account_prepare_query(query);
4113 rc = _account_query_step(hstmt);
4115 account_type_s *account_type_record = NULL;
4117 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4120 while(rc == SQLITE_ROW) {
4121 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4123 if (account_type_record == NULL) {
4124 ACCOUNT_FATAL("malloc Failed");
4128 ACCOUNT_MEMSET(account_type_record, 0x00, sizeof(account_type_s));
4129 _account_type_convert_column_to_account_type(hstmt, account_type_record);
4130 account_type_list = g_list_append(account_type_list, account_type_record);
4131 rc = _account_query_step(hstmt);
4135 _account_query_finalize(hstmt);
4140 for (iter = account_type_list; iter != NULL; iter = g_list_next(iter)) {
4141 account_type_s *account_type = NULL;
4142 account_type = (account_type_s*)iter->data;
4143 account_type_query_label_by_app_id(_account_get_label_text_cb,account_type->app_id,(void*)account_type);
4144 callback((account_type_h)account_type, user_data);
4148 error_code = ACCOUNT_ERROR_NONE;
4151 if (hstmt != NULL) {
4152 _account_query_finalize(hstmt);
4155 if (account_type_list) {
4156 _account_type_glist_free(account_type_list);
4157 account_type_list = NULL;
4163 // output parameter label must be free
4164 int account_type_query_label_by_locale(const char* app_id, const char* locale, char** label)
4166 int error_code = ACCOUNT_ERROR_NONE;
4167 account_stmt hstmt = NULL;
4168 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4169 int rc = 0, binding_count = 1;
4171 ACCOUNT_RETURN_VAL((app_id != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO APP ID"));
4172 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4173 ACCOUNT_RETURN_VAL((label != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("label char is null"));
4175 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4177 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, locale);
4178 hstmt = _account_prepare_query(query);
4180 _account_query_bind_text(hstmt, binding_count++, app_id);
4182 rc = _account_query_step(hstmt);
4183 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4185 label_s* label_record = NULL;
4187 while (rc == SQLITE_ROW) {
4188 label_record = (label_s*) malloc(sizeof(label_s));
4190 if (label_record == NULL) {
4191 ACCOUNT_FATAL("malloc Failed");
4195 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
4197 _account_type_convert_column_to_label(hstmt,label_record);
4199 *label = _account_get_text(label_record->label);
4201 _account_type_free_label_items(label_record);
4202 _ACCOUNT_FREE(label_record);
4204 rc = _account_query_step(hstmt);
4207 _account_query_finalize(hstmt);
4210 error_code = ACCOUNT_ERROR_NONE;
4213 if (hstmt != NULL) {
4214 _account_query_finalize(hstmt);
4218 pthread_mutex_unlock(&account_mutex);
4222 static int _account_insert_custom(account_s *account, int account_id)
4225 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4226 account_stmt hstmt = NULL;
4228 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4230 if (g_slist_length( account->custom_list)==0) {
4231 ACCOUNT_ERROR( "_account_insert_custom, no custom data\n");
4232 return ACCOUNT_ERROR_NONE;
4235 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
4237 rc = _account_get_record_count(query);
4240 ACCOUNT_ERROR( "_account_insert_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
4241 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4248 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
4251 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4252 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s (AccountId, AppId, Key, Value) VALUES "
4253 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
4255 hstmt = _account_prepare_query(query);
4257 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4259 account_custom_s* custom_data = NULL;
4260 custom_data = (account_custom_s*)iter->data;
4261 ACCOUNT_VERBOSE( "account_custom_s->key = %s, account_custom_s->value = %s \n", custom_data->key, custom_data->value);
4263 ret = _account_query_bind_int(hstmt, count++, account_id);
4264 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
4265 ret = _account_query_bind_text(hstmt, count++, account->package_name);
4266 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4267 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
4268 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4269 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
4270 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4272 rc = _account_query_step(hstmt);
4274 if (rc != SQLITE_DONE) {
4275 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4279 _account_query_finalize(hstmt);
4284 ACCOUNT_VERBOSE( "_account_insert_custom() DONE\n");
4286 return ACCOUNT_ERROR_NONE;
4289 static int _account_update_custom(account_s *account, int account_id)
4292 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4293 account_stmt hstmt = NULL;
4295 ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4297 if (g_slist_length( account->custom_list)==0) {
4298 ACCOUNT_ERROR( "_account_update_custom, no custom data\n");
4299 return ACCOUNT_ERROR_NONE;
4302 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
4304 rc = _account_get_record_count(query);
4307 ACCOUNT_ERROR( "_account_update_custom : related account item is not existed rc=%d , %s", rc, _account_db_err_msg());
4308 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
4311 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4313 ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId=? ", ACCOUNT_CUSTOM_TABLE);
4314 hstmt = _account_prepare_query(query);
4316 _account_query_bind_int(hstmt, count++, (int)account_id);
4317 rc = _account_query_step(hstmt);
4319 if (rc != SQLITE_DONE) {
4320 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4321 return ACCOUNT_ERROR_DB_FAILED;
4323 _account_query_finalize(hstmt);
4328 for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
4331 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4332 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AccountId, AppId, Key, Value) VALUES "
4333 "(?, ?, ?, ?) ", ACCOUNT_CUSTOM_TABLE);
4335 hstmt = _account_prepare_query(query);
4337 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4339 account_custom_s* custom_data = NULL;
4340 custom_data = (account_custom_s*)iter->data;
4342 ret = _account_query_bind_int(hstmt, count++, (int)account_id);
4343 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Int binding fail"));
4344 ret = _account_query_bind_text(hstmt, count++, (char*)account->package_name);
4345 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4346 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->key);
4347 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4348 ret = _account_query_bind_text(hstmt, count++, (char*)custom_data->value);
4349 ACCOUNT_RETURN_VAL((ret == ACCOUNT_ERROR_NONE), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Text binding fail"));
4351 rc = _account_query_step(hstmt);
4353 if (rc != SQLITE_DONE) {
4354 ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4358 _account_query_finalize(hstmt);
4363 ACCOUNT_VERBOSE( "_account_update_custom() DONE\n");
4365 return ACCOUNT_ERROR_NONE;
4368 static int _account_query_custom_by_account_id(account_custom_cb cb_func, int account_id, void *user_data )
4370 int error_code = ACCOUNT_ERROR_NONE;
4371 account_stmt hstmt = NULL;
4372 char query[ACCOUNT_SQL_LEN_MAX] = {0, };
4375 ACCOUNT_RETURN_VAL((account_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT INDEX IS LESS THAN 0"));
4376 ACCOUNT_RETURN_VAL((cb_func != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("NO CALLBACK FUNCTION"));
4377 ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
4379 ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4381 ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
4382 hstmt = _account_prepare_query(query);
4384 rc = _account_query_step(hstmt);
4385 ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4387 account_custom_s* custom_record = NULL;
4389 while (rc == SQLITE_ROW) {
4390 custom_record = (account_custom_s*) malloc(sizeof(account_custom_s));
4392 if (custom_record == NULL) {
4393 ACCOUNT_FATAL("malloc Failed");
4397 ACCOUNT_MEMSET(custom_record, 0x00, sizeof(account_custom_s));
4399 _account_convert_column_to_custom(hstmt, custom_record);
4401 cb_func(custom_record->key, custom_record->value, user_data);
4403 _account_custom_item_free(custom_record);
4404 _ACCOUNT_FREE(custom_record);
4406 rc = _account_query_step(hstmt);
4409 _account_query_finalize(hstmt);
4412 error_code = ACCOUNT_ERROR_NONE;
4415 if (hstmt != NULL) {
4416 _account_query_finalize(hstmt);
4420 pthread_mutex_unlock(&account_mutex);