Tizen 2.1 base
[framework/pim/libaccounts-svc.git] / src / account.c
1 /*
2  *  account
3  *
4  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Wonyoung Lee <wy1115.lee@samsung.com>, Sungchan Kim <sungchan81.kim@samsung.com>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <account.h>
26 #include <glib.h>
27 #include <db-util.h>
28 #include <pthread.h>
29 #include <assert.h>
30 #include <account-private.h>
31 #include <vconf.h>
32 #include <app.h>
33
34 static sqlite3* g_hAccountDB = NULL;
35 static int              g_refCntDB = 0;
36 pthread_mutex_t account_mutex = PTHREAD_MUTEX_INITIALIZER;
37
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 );
45
46 static const char *_account_db_err_msg()
47 {
48         assert(NULL != g_hAccountDB);
49         return sqlite3_errmsg(g_hAccountDB);
50 }
51
52 static void _account_insert_delete_update_notification_send(char *noti_name)
53 {
54         if (!noti_name) {
55                 ACCOUNT_ERROR("Noti Name is NULL!!!!!!\n");
56                 return;
57         }
58
59         ACCOUNT_DEBUG("Sending notification with value %s\n", noti_name);
60
61         if (vconf_set_str(VCONFKEY_ACCOUNT_MSG_STR, noti_name) == 0) {
62                 ACCOUNT_VERBOSE("Vconf MSG Str set SUCCESS !!!!!!\n");;
63         } else {
64                 ACCOUNT_ERROR("Vconf MSG Str set FAILED !!!!!!\n");;
65         }
66 }
67
68 static int _account_get_record_count(char* query)
69 {
70         int rc = -1;
71         int ncount = 0;
72         account_stmt pStmt = NULL;
73
74         assert(NULL != query);
75         assert(NULL != g_hAccountDB);
76         rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
77
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;
83         }
84
85         ncount = sqlite3_column_int(pStmt, 0);
86
87         ACCOUNT_VERBOSE("count : %d, End", ncount);
88         sqlite3_finalize(pStmt);
89
90         return ncount;
91 }
92
93 static int _account_execute_query(char *query)
94 {
95         int rc = -1;
96         char* pszErrorMsg = NULL;
97
98         assert(NULL != query);
99         assert(NULL != g_hAccountDB);
100
101         ACCOUNT_INFO("query : %s", query);
102
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);
107         }
108
109         return rc;
110 }
111
112 static int _account_begin_transaction(void)
113 {
114         int ret = -1;
115
116         ret = _account_execute_query("BEGIN IMMEDIATE TRANSACTION");
117
118         if (ret != SQLITE_OK) {
119                 ACCOUNT_FATAL("_account_svc_begin_transaction fail :: %d", ret);
120                 return ACCOUNT_ERROR_DB_FAILED;
121         }
122
123    return ACCOUNT_ERROR_NONE;
124 }
125
126 static int _account_end_transaction(bool is_success)
127 {
128         int ret = -1;
129
130         if (is_success == true) {
131                 ret = _account_execute_query("COMMIT TRANSACTION");
132         } else {
133                 ret = _account_execute_query("ROLLBACK TRANSACTION");
134         }
135
136         if (ret != SQLITE_OK) {
137                 ACCOUNT_FATAL("_account_svc_end_transaction fail :: %d", ret);
138                 return ACCOUNT_ERROR_DB_FAILED;
139         }
140
141    return ACCOUNT_ERROR_NONE;
142 }
143
144 static int _account_create_all_tables(void)
145 {
146         int rc = -1;
147         int error_code = ACCOUNT_ERROR_NONE;
148         char    query[ACCOUNT_SQL_LEN_MAX] = {0, };
149
150
151         ACCOUNT_MEMSET(query, 0, sizeof(query));
152
153         ACCOUNT_VERBOSE("_account_create_all_tables begin");
154
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);
158         if (rc <= 0) {
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()));
164         }
165
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);
170         if (rc <= 0) {
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()));
176         }
177
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);
182         if (rc <= 0) {
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()));
188         }
189
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);
194         if (rc <= 0) {
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()));
200         }
201
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);
206         if (rc <= 0) {
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()));
212         }
213
214         return error_code;
215 }
216
217 static bool _account_check_is_all_table_exists()
218 {
219         int     rc = 0;
220         char    query[ACCOUNT_SQL_LEN_MAX] = {0,};
221         ACCOUNT_MEMSET(query, 0, sizeof(query));
222
223         ACCOUNT_VERBOSE("_account_check_is_all_table_exists");
224
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);
230                 return FALSE;
231         }
232
233         ACCOUNT_VERBOSE("END of _account_check_is_all_table_exists\n");
234
235         return TRUE;
236 }
237
238 static int _account_db_open(void)
239 {
240         int     rc = 0;
241         bool is_success = false;
242         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
243
244         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
245
246         if (!g_hAccountDB) {
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));
249
250                 g_refCntDB++;
251                 is_success = true;
252                 ACCOUNT_INFO("_account_db_open: The database connected. refcnt=%d ", g_refCntDB);
253         } else {
254                 g_refCntDB++;
255                 is_success = true;
256                 ACCOUNT_INFO("The database already connected. refcnt=%d ", g_refCntDB);
257         }
258
259         return ACCOUNT_ERROR_NONE;
260 }
261
262 static int _account_db_close(void)
263 {
264         int     rc = 0;
265         int ret = -1;
266
267         if (g_hAccountDB) {
268                 if (g_refCntDB > 0) {
269                         g_refCntDB--;
270                         ACCOUNT_INFO("_account_svc_db_close: The database disconnected. refcnt=%d ", g_refCntDB);
271                 }
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));
275                         g_hAccountDB = NULL;
276                         ACCOUNT_INFO( "_account_svc_db_close: The database disconnected really. ");
277                 }
278                 ret = ACCOUNT_ERROR_NONE;
279         } else {
280                 ACCOUNT_ERROR( "_account_svc_db_close: No handle(). refcnt=%d ", g_refCntDB);
281                 ret = ACCOUNT_ERROR_DB_FAILED;
282         }
283
284         return ret;
285 }
286
287 static int _account_connect(void)
288 {
289         int error_code = ACCOUNT_ERROR_NONE;
290
291         pthread_mutex_lock(&account_mutex);
292
293         ACCOUNT_VERBOSE("db path = %s\n", ACCOUNT_DB_NAME);
294
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;
300         }
301
302         if (FALSE == _account_check_is_all_table_exists())
303                 error_code = _account_create_all_tables();
304
305         pthread_mutex_unlock(&account_mutex);
306         return ACCOUNT_ERROR_NONE;
307 }
308
309 int account_connect (void)
310 {
311         return _account_connect();
312 }
313
314 static int _account_disconnect(void)
315 {
316         int error_code = ACCOUNT_ERROR_NONE;
317
318         pthread_mutex_lock(&account_mutex);
319         ACCOUNT_INFO("db path = %s have been closed!!!\n", ACCOUNT_DB_NAME);
320
321         error_code = _account_db_close();
322         pthread_mutex_unlock(&account_mutex);
323
324         return error_code;
325 }
326
327 int account_disconnect (void)
328 {
329         return _account_disconnect();
330 }
331
332 static int _account_free_capability_items(account_capability_s *data)
333 {
334         _ACCOUNT_FREE(data->type);
335         _ACCOUNT_FREE(data->package_name);
336         _ACCOUNT_FREE(data->user_name);
337
338         return ACCOUNT_ERROR_NONE;
339 }
340
341 static int _account_custom_item_free(account_custom_s *data)
342 {
343         _ACCOUNT_FREE(data->app_id);
344         _ACCOUNT_FREE(data->key);
345         _ACCOUNT_FREE(data->value);
346
347         return ACCOUNT_ERROR_NONE;
348 }
349
350 static int _account_custom_gslist_free(GSList* list)
351 {
352         ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL"));
353
354         GSList* iter;
355
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);
360         }
361
362         g_slist_free(list);
363         list = NULL;
364
365         return ACCOUNT_ERROR_NONE;
366 }
367
368 static int _account_free_account_items(account_s *data)
369 {
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);
377
378         int i;
379         for(i=0;i<USER_TXT_CNT;i++)
380                 _ACCOUNT_FREE(data->user_data_txt[i]);
381
382         _account_gslist_free(data->capablity_list);
383         _account_glist_free(data->account_list);
384         _account_custom_gslist_free(data->custom_list);
385
386         return ACCOUNT_ERROR_NONE;
387 }
388
389 static int _account_gslist_free(GSList* list)
390 {
391         ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL"));
392
393         GSList* iter;
394
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);
399         }
400
401         g_slist_free(list);
402         list = NULL;
403
404         return ACCOUNT_ERROR_NONE;
405 }
406
407 static int _account_glist_free(GList* list)
408 {
409         ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Glist is NULL"));
410
411         GList* iter;
412
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);
417         }
418
419         g_list_free(list);
420         list = NULL;
421
422         return ACCOUNT_ERROR_NONE;
423 }
424
425 static gboolean _account_check_duplicated(account_s *data)
426 {
427         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
428         int count = 0;
429
430         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
431
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);
434
435         count = _account_get_record_count(query);
436         if (count > 0) {
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 );
439                 return TRUE;
440         }
441
442         return FALSE;
443 }
444
445 static int _account_get_next_sequence(char *pszName)
446 {
447         int                     rc = 0;
448         account_stmt    pStmt = NULL;
449         int                     max_seq = 0;
450         char                    szQuery[ACCOUNT_SQL_LEN_MAX] = {0,};
451
452         ACCOUNT_VERBOSE( "[Enter] pszName:%s\n", pszName);
453
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);
457
458         rc = sqlite3_step(pStmt);
459         max_seq = sqlite3_column_int(pStmt, 0);
460         max_seq++;
461
462         ACCOUNT_VERBOSE( "sqlite3_column_int, rc=%d, max_seq=%d\n", rc, max_seq);
463
464         /*Finalize Statement*/
465         rc = sqlite3_finalize(pStmt);
466         pStmt = NULL;
467
468         return max_seq;
469 }
470
471 static account_stmt _account_prepare_query(char *query)
472 {
473         int                     rc = -1;
474         account_stmt    pStmt = NULL;
475
476         ACCOUNT_RETURN_VAL((query != NULL), {}, NULL, ("query is NULL"));
477
478         ACCOUNT_INFO( "prepare query : %s", query);
479
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()));
482
483         return pStmt;
484 }
485
486 static int _account_query_bind_int(account_stmt pStmt, int pos, int num)
487 {
488         assert(NULL != pStmt);
489         assert(pos > -1);
490         return sqlite3_bind_int(pStmt, pos, num);
491 }
492
493 static int _account_query_bind_text(account_stmt pStmt, int pos, const char *str)
494 {
495         assert(NULL != pStmt);
496
497         if(str)
498                 return sqlite3_bind_text(pStmt, pos, (const char*)str, strlen(str), SQLITE_STATIC);
499         else
500                 return sqlite3_bind_null(pStmt, pos);
501 }
502
503 static int _account_convert_account_to_sql(account_s *account, account_stmt hstmt, char *sql_value)
504 {
505         ACCOUNT_VERBOSE( "_account_convert_account_to_sql");
506         int count = 1;
507
508         /*Caution : Keep insert query orders.*/
509
510         /* 1. user name*/
511         _account_query_bind_text(hstmt, count++, (char*)account->user_name);
512
513         /* 2. email address*/
514         _account_query_bind_text(hstmt, count++, (char*)account->email_address);
515
516         /* 3. display name*/
517         _account_query_bind_text(hstmt, count++, (char*)account->display_name);
518
519         /* 4. icon path*/
520         _account_query_bind_text(hstmt, count++, (char*)account->icon_path);
521
522         /* 5. source*/
523         _account_query_bind_text(hstmt, count++, (char*)account->source);
524
525         /* 6. package name*/
526         _account_query_bind_text(hstmt, count++, (char*)account->package_name);
527
528         /* 7. access token*/
529         _account_query_bind_text(hstmt, count++, (char*)account->access_token);
530
531         /* 8. domain name*/
532         _account_query_bind_text(hstmt, count++, (char*)account->domain_name);
533
534         /* 9. auth type*/
535         _account_query_bind_int(hstmt, count++, account->auth_type);
536
537         /* 10. secret */
538         _account_query_bind_int(hstmt, count++, account->secret);
539
540         /* 11. sync_support */
541         _account_query_bind_int(hstmt, count++, account->sync_support);
542
543         int i;
544
545         /* 12. user text*/
546         for(i=0; i< USER_TXT_CNT; i++)
547                 _account_query_bind_text(hstmt, count++, (char*)account->user_data_txt[i]);
548
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]);
552
553         return count;
554 }
555
556 static void _account_query_finalize(account_stmt pStmt)
557 {
558         int rc = -1;
559
560         if (!pStmt) {
561                 ACCOUNT_FATAL( "pStmt is NULL");
562                 return;
563         }
564
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());
568         }
569
570         ACCOUNT_VERBOSE( "sqlite3_finalize finish");
571 }
572
573 static int _account_query_step(account_stmt pStmt)
574 {
575         assert(NULL != pStmt);
576         return sqlite3_step(pStmt);
577 }
578
579
580
581 static int _do_account_owner_existance_check()
582 {
583         /* TODO check owner*/
584         return ACCOUNT_ERROR_NONE;
585 }
586
587 static int _account_execute_insert_query(account_s *account)
588 {
589         int                             rc = 0;
590         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
591         int                             error_code = ACCOUNT_ERROR_NONE;
592         account_stmt    hstmt = NULL;
593
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;
597         }
598
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;
602         }
603
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);
609
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()));
612
613         _account_convert_account_to_sql(account, hstmt, query);
614
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;
619         }
620
621         _account_query_finalize(hstmt);
622         hstmt = NULL;
623
624         return error_code;
625 }
626
627 static int _account_insert_capability(account_s *account, int account_id)
628 {
629         int                     rc, count = 1;
630         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
631         account_stmt    hstmt = NULL;
632
633         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
634
635         if (g_slist_length( account->capablity_list)==0) {
636                 ACCOUNT_ERROR( "_account_insert_capability, no capability\n");
637                 return ACCOUNT_ERROR_NONE;
638         }
639
640         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
641
642         rc = _account_get_record_count(query);
643
644         if (rc <= 0) {
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;
647         }
648
649         /* insert query*/
650
651         GSList *iter;
652
653         for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
654                 int rc, ret;
655                 count = 1;
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);
659
660                 hstmt = _account_prepare_query(query);
661
662                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
663
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);
667
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"));
678
679                 rc = _account_query_step(hstmt);
680
681                 if (rc != SQLITE_DONE) {
682                         ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
683                         break;
684                 }
685
686                 _account_query_finalize(hstmt);
687                 hstmt = NULL;
688
689         }
690
691         ACCOUNT_VERBOSE( "_account_insert_capability() DONE\n");
692
693         return ACCOUNT_ERROR_NONE;
694 }
695
696 static int _account_update_capability(account_s *account, int account_id)
697 {
698         int                     rc, count = 1;
699         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
700         account_stmt    hstmt = NULL;
701
702         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
703
704         if (g_slist_length( account->capablity_list)==0) {
705                 ACCOUNT_ERROR( "_account_insert_capability, no capability\n");
706                 return ACCOUNT_ERROR_NONE;
707         }
708
709         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
710
711         rc = _account_get_record_count(query);
712
713         if (rc <= 0) {
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;
716         }
717
718         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
719
720         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id=? ", CAPABILITY_TABLE);
721         hstmt = _account_prepare_query(query);
722         count = 1;
723         _account_query_bind_int(hstmt, count++, (int)account_id);
724         rc = _account_query_step(hstmt);
725
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;
729         }
730         _account_query_finalize(hstmt);
731         hstmt = NULL;
732
733         GSList *iter;
734
735         for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
736                 int rc, ret;
737                 count = 1;
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);
741
742                 hstmt = _account_prepare_query(query);
743
744                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
745
746                 account_capability_s* cap_data = NULL;
747                 cap_data = (account_capability_s*)iter->data;
748
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"));
759
760                 rc = _account_query_step(hstmt);
761
762                 if (rc != SQLITE_DONE) {
763                         ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
764                         break;
765                 }
766
767                 _account_query_finalize(hstmt);
768                 hstmt = NULL;
769
770         }
771
772         ACCOUNT_DEBUG( "_account_insert_capability() DONE\n");
773
774         return ACCOUNT_ERROR_NONE;
775 }
776
777 static int _account_update_capability_by_user_name(account_s *account, char *user_name, char *package_name )
778 {
779         int                     rc, count = 1;
780         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
781         account_stmt    hstmt = NULL;
782
783         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
784
785         if (g_slist_length( account->capablity_list)==0) {
786                 ACCOUNT_ERROR( "_account_insert_capability, no capability\n");
787                 return ACCOUNT_ERROR_NONE;
788         }
789
790         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where package_name=%s and user_name=%s", ACCOUNT_TABLE, package_name, user_name);
791
792         rc = _account_get_record_count(query);
793
794         if (rc <= 0) {
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;
797         }
798
799         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
800
801         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name=? and user_name=? ", CAPABILITY_TABLE);
802         hstmt = _account_prepare_query(query);
803         count = 1;
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;
810         }
811
812         _account_query_finalize(hstmt);
813         hstmt = NULL;
814
815         GSList* iter;
816
817         for (iter = account->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
818                 int rc, ret;
819                 count = 1;
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);
823
824                 hstmt = _account_prepare_query(query);
825
826                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
827
828                 account_capability_s* cap_data = NULL;
829                 cap_data = (account_capability_s*)iter->data;
830
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"));
841
842                 rc = _account_query_step(hstmt);
843
844                 if (rc != SQLITE_DONE) {
845                         ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
846                         break;
847                 }
848
849                 _account_query_finalize(hstmt);
850                 hstmt = NULL;
851
852         }
853
854         ACCOUNT_VERBOSE( "_account_insert_capability() DONE\n");
855
856         return ACCOUNT_ERROR_NONE;
857 }
858
859 static int _account_query_table_column_int(account_stmt pStmt, int pos)
860 {
861         assert(NULL != pStmt);
862         assert(pos > -1);
863         return sqlite3_column_int(pStmt, pos);
864 }
865
866 static char *_account_query_table_column_text(account_stmt pStmt, int pos)
867 {
868         assert(NULL != pStmt);
869         assert(pos > -1);
870         return (char *)sqlite3_column_text(pStmt, pos);
871 }
872
873 static void _account_db_data_to_text(char *textbuf, char **output)
874 {
875         if (textbuf && strlen(textbuf)>0) {
876                 if (*output) {
877                         free(*output);
878                         *output = NULL;
879                 }
880                 *output = strdup(textbuf);
881         } else {
882                 ACCOUNT_WARNING("_account_db_data_to_text : no text");
883         }
884 }
885
886 static void _account_convert_column_to_account(account_stmt hstmt, account_s *account_record)
887 {
888         char    *textbuf = NULL;
889
890         account_record->id = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_ID);
891
892         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_USER_NAME);
893         _account_db_data_to_text(textbuf, &(account_record->user_name));
894
895         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_EMAIL_ADDRESS);
896         _account_db_data_to_text(textbuf, &(account_record->email_address));
897
898         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DISPLAY_NAME);
899         _account_db_data_to_text(textbuf, &(account_record->display_name));
900
901         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ICON_PATH);
902         _account_db_data_to_text(textbuf, &(account_record->icon_path));
903
904         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_SOURCE);
905         _account_db_data_to_text(textbuf, &(account_record->source));
906
907         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_PACKAGE_NAME);
908         _account_db_data_to_text(textbuf, &(account_record->package_name));
909
910         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_ACCESS_TOKEN);
911         _account_db_data_to_text(textbuf, &(account_record->access_token));
912
913         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_FIELD_DOMAIN_NAME);
914         _account_db_data_to_text(textbuf, &(account_record->domain_name));
915
916         account_record->auth_type = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_AUTH_TYPE);
917
918         account_record->secret = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SECRET);
919
920         account_record->sync_support = _account_query_table_column_int(hstmt, ACCOUNT_FIELD_SYNC_SUPPORT);
921
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]));
924
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]));
927
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]));
930
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]));
933
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]));
936
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);
942
943         ACCOUNT_VERBOSE("END _account_convert_column_to_account");
944 }
945
946 static void _account_convert_column_to_capability(account_stmt hstmt, account_capability_s *capability_record)
947 {
948         char *textbuf = NULL;
949
950         capability_record->id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ID);
951
952         textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_KEY);
953         _account_db_data_to_text(textbuf, &(capability_record->type));
954
955         capability_record->value = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_VALUE);
956
957         textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_PACKAGE_NAME);
958         _account_db_data_to_text(textbuf, &(capability_record->package_name));
959
960         textbuf = _account_query_table_column_text(hstmt, CAPABILITY_FIELD_USER_NAME);
961         _account_db_data_to_text(textbuf, &(capability_record->user_name));
962
963         capability_record->account_id = _account_query_table_column_int(hstmt, CAPABILITY_FIELD_ACCOUNT_ID);
964
965         ACCOUNT_VERBOSE("END _account_convert_column_to_capability");
966 }
967
968 static void _account_convert_column_to_custom(account_stmt hstmt, account_custom_s *custom_record)
969 {
970         char *textbuf = NULL;
971
972         custom_record->account_id = _account_query_table_column_int(hstmt, ACCOUNT_CUSTOM_FIELD_ACCOUNT_ID);
973
974         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_APP_ID);
975         _account_db_data_to_text(textbuf, &(custom_record->app_id));
976
977         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_KEY);
978         _account_db_data_to_text(textbuf, &(custom_record->key));
979
980         textbuf = _account_query_table_column_text(hstmt, ACCOUNT_CUSTOM_FIELD_VALUE);
981         _account_db_data_to_text(textbuf, &(custom_record->value));
982
983         ACCOUNT_VERBOSE("END _account_convert_column_to_custom");
984 }
985
986 bool _account_get_capability_text_cb(const char* capability_type, account_capability_state_e capability_value, void *user_data)
987 {
988         account_s *data = (account_s*)user_data;
989
990         account_capability_s *cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
991
992         if (cap_data == NULL)
993                 return FALSE;
994         ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
995
996         cap_data->type = _account_get_text(capability_type);
997         cap_data->value = capability_value;
998
999         data->capablity_list = g_slist_append(data->capablity_list, (gpointer)cap_data);
1000
1001         ACCOUNT_VERBOSE("_account_get_capability_text_cb :: %d\n", capability_type);
1002
1003         return TRUE;
1004 }
1005
1006
1007 bool _account_get_custom_text_cb(char* key, char* value, void *user_data)
1008 {
1009         account_s *data = (account_s*)user_data;
1010
1011         account_custom_s *custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
1012
1013         if (custom_data == NULL) {
1014                 ACCOUNT_DEBUG("_account_get_custom_text_cb :: malloc fail\n");
1015                 return FALSE;
1016         }
1017         ACCOUNT_MEMSET(custom_data, 0, sizeof(account_custom_s));
1018
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);
1023
1024         data->custom_list = g_slist_append(data->custom_list, (gpointer)custom_data);
1025
1026         return TRUE;
1027 }
1028
1029
1030 static char *_account_get_text(const char *text_data)
1031 {
1032         char *text_value = NULL;
1033
1034         if (text_data != NULL) {
1035                 text_value = strdup(text_data);
1036                 ACCOUNT_VERBOSE("text_value = %s", text_value);
1037         }
1038         return text_value;
1039 }
1040
1041 static int _account_update_account_by_user_name(account_s *account, char *user_name, char *package_name)
1042 {
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;
1047
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"));
1050
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;
1054         }
1055
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);
1058
1059         count = _account_get_record_count(query);
1060         if (count <= 0) {
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;
1064         }
1065
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);
1071
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()));
1074
1075         binding_count = _account_convert_account_to_sql(account, hstmt, query);
1076
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());
1082         }
1083         _account_query_finalize(hstmt);
1084         hstmt = NULL;
1085
1086         /*update capability*/
1087         error_code = _account_update_capability_by_user_name(account, user_name, package_name);
1088
1089         return error_code;
1090 }
1091
1092 int account_insert_to_db(account_h account, int *account_id)
1093 {
1094         int             error_code = ACCOUNT_ERROR_NONE;
1095
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"));
1099
1100         account_s *data = (account_s*)account;
1101
1102         pthread_mutex_lock(&account_mutex);
1103
1104         if (_account_check_duplicated(data)) {
1105                 error_code = ACCOUNT_ERROR_DUPLICATED;
1106         } else {
1107                 *account_id = _account_get_next_sequence(ACCOUNT_TABLE);
1108
1109                 error_code = _account_execute_insert_query(data);
1110
1111                 if (error_code != ACCOUNT_ERROR_NONE)
1112                         *account_id = -1;
1113         }
1114
1115         ACCOUNT_VERBOSE( "_account_execute_insert_query, insert error_code : %d", error_code);
1116
1117         _account_insert_capability(data, *account_id);
1118         _account_insert_custom(data, *account_id);
1119
1120         pthread_mutex_unlock(&account_mutex);
1121         _account_insert_delete_update_notification_send(ACCOUNT_NOTI_NAME_INSERT);
1122
1123         return ACCOUNT_ERROR_NONE;
1124
1125 }
1126
1127 int account_create(account_h *account)
1128 {
1129         if (!account) {
1130                 ACCOUNT_ERROR("(%s)-(%d) account is NULL.\n", __FUNCTION__, __LINE__);
1131                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1132         }
1133
1134         account_s *data = (account_s*)malloc(sizeof(account_s));
1135
1136         if (data == NULL) {
1137                 ACCOUNT_FATAL("Memory Allocation Failed");
1138                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
1139         }
1140         ACCOUNT_MEMSET(data, 0, sizeof(account_s));
1141
1142         ACCOUNT_VERBOSE("create handle=%p\n", *account);
1143
1144         /*Setting account as visible by default*/
1145         data->secret = ACCOUNT_SECRECY_VISIBLE;
1146
1147         /*Setting account as not supporting sync by default*/
1148         data->sync_support = ACCOUNT_SYNC_NOT_SUPPORT;
1149
1150         *account = (account_h)data;
1151
1152         return ACCOUNT_ERROR_NONE;
1153 }
1154
1155 int account_set_user_name(account_h account, const char *user_name)
1156 {
1157         if (!account) {
1158                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1159                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1160         }
1161
1162         if (!user_name) {
1163                 ACCOUNT_ERROR("(%s)-(%d) user_name is NULL.\n", __FUNCTION__, __LINE__);
1164                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1165         }
1166
1167         account_s *data = (account_s*)account;
1168
1169         _ACCOUNT_FREE(data->user_name);
1170         data->user_name = _account_get_text(user_name);
1171
1172         ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->user_name, user_name);
1173
1174         return ACCOUNT_ERROR_NONE;
1175 }
1176
1177 int account_set_display_name(account_h account, const char *display_name)
1178 {
1179         if (!account) {
1180                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1181                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1182         }
1183
1184         if (!display_name) {
1185                 ACCOUNT_ERROR("(%s)-(%d) display_name is NULL.\n", __FUNCTION__, __LINE__);
1186                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1187         }
1188
1189         account_s *data = (account_s*)account;
1190
1191         _ACCOUNT_FREE(data->display_name);
1192         data->display_name = _account_get_text(display_name);
1193
1194         ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->display_name, display_name);
1195
1196         return ACCOUNT_ERROR_NONE;
1197 }
1198
1199 int account_set_email_address(account_h account, const char *email_address)
1200 {
1201         if (!account) {
1202                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1203                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1204         }
1205
1206         if (!email_address) {
1207                 ACCOUNT_ERROR("(%s)-(%d) email_address is NULL.\n", __FUNCTION__, __LINE__);
1208                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1209         }
1210
1211         account_s *data = (account_s*)account;
1212
1213         _ACCOUNT_FREE(data->email_address);
1214         data->email_address = _account_get_text(email_address);
1215
1216         ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->email_address, email_address);
1217
1218         return ACCOUNT_ERROR_NONE;
1219 }
1220
1221 int account_set_icon_path(account_h account, const char *icon_path)
1222 {
1223         if (!account) {
1224                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1225                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1226         }
1227
1228         if (!icon_path) {
1229                 ACCOUNT_ERROR("(%s)-(%d) icon_path is NULL.\n", __FUNCTION__, __LINE__);
1230                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1231         }
1232
1233         account_s *data = (account_s*)account;
1234
1235         _ACCOUNT_FREE(data->icon_path);
1236         data->icon_path = _account_get_text(icon_path);
1237
1238         ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->icon_path, icon_path);
1239
1240         return ACCOUNT_ERROR_NONE;
1241 }
1242
1243 int account_set_source(account_h account, const char *source)
1244 {
1245         if (!account) {
1246                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1247                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1248         }
1249
1250         if (!source) {
1251                 ACCOUNT_ERROR("(%s)-(%d) source is NULL.\n", __FUNCTION__, __LINE__);
1252                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1253         }
1254         account_s *data = (account_s*)account;
1255
1256         _ACCOUNT_FREE(data->source);
1257         data->source = _account_get_text(source);
1258
1259         ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->source, source);
1260
1261         return ACCOUNT_ERROR_NONE;
1262 }
1263
1264 int account_set_package_name(account_h account, const char *package_name)
1265 {
1266         if (!account) {
1267                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1268                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1269         }
1270
1271         if (!package_name) {
1272                 ACCOUNT_ERROR("(%s)-(%d) package_name is NULL.\n", __FUNCTION__, __LINE__);
1273                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1274         }
1275
1276         account_s *data = (account_s*)account;
1277
1278         _ACCOUNT_FREE(data->package_name);
1279         data->package_name = _account_get_text(package_name);
1280
1281         ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->package_name, package_name);
1282
1283         return ACCOUNT_ERROR_NONE;
1284 }
1285
1286 int account_set_domain_name(account_h account, const char *domain_name)
1287 {
1288         if (!account) {
1289                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1290                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1291         }
1292
1293         if (!domain_name) {
1294                 ACCOUNT_ERROR("(%s)-(%d) domain_name is NULL.\n", __FUNCTION__, __LINE__);
1295                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1296         }
1297         account_s *data = (account_s*)account;
1298
1299         _ACCOUNT_FREE(data->domain_name);
1300         data->domain_name = _account_get_text(domain_name);
1301
1302         ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->domain_name, domain_name);
1303
1304         return ACCOUNT_ERROR_NONE;
1305 }
1306
1307 int account_set_access_token(account_h account, const char *access_token)
1308 {
1309         if (!account) {
1310                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1311                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1312         }
1313
1314         if (!access_token) {
1315                 ACCOUNT_ERROR("(%s)-(%d) access_token is NULL.\n", __FUNCTION__, __LINE__);
1316                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1317         }
1318
1319         account_s *data = (account_s*)account;
1320
1321         _ACCOUNT_FREE(data->access_token);
1322         data->access_token = _account_get_text(access_token);
1323
1324         ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->access_token, access_token);
1325
1326         return ACCOUNT_ERROR_NONE;
1327 }
1328
1329 int account_set_user_text(account_h account, int index, const char *user_txt)
1330 {
1331         if (!account) {
1332                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1333                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1334         }
1335
1336         if (!user_txt) {
1337                 ACCOUNT_ERROR("(%s)-(%d) user_txt is NULL.\n", __FUNCTION__, __LINE__);
1338                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1339         }
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;
1343         }
1344
1345         account_s *data = (account_s*)account;
1346
1347         _ACCOUNT_FREE(data->user_data_txt[index]);
1348         data->user_data_txt[index] = _account_get_text(user_txt);
1349
1350         ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->user_data_txt[index], user_txt);
1351
1352         return ACCOUNT_ERROR_NONE;
1353 }
1354
1355 int account_set_custom(account_h account, const char* key, const char* value)
1356 {
1357         if (!account) {
1358                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1359                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1360         }
1361
1362         if (!key) {
1363                 ACCOUNT_ERROR("(%s)-(%d) key is NULL.\n", __FUNCTION__, __LINE__);
1364                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1365         }
1366
1367         if (!value) {
1368                 ACCOUNT_ERROR("(%s)-(%d) value is NULL.\n", __FUNCTION__, __LINE__);
1369                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1370         }
1371
1372         account_s *data = (account_s*)account;
1373
1374         GSList *iter;
1375         bool b_is_new = TRUE;
1376
1377         for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) {
1378
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);
1382
1383                 if(!strcmp(custom_data->key, key)) {
1384                         _ACCOUNT_FREE(custom_data->value);
1385                         custom_data->value = _account_get_text(value);
1386                         b_is_new = FALSE;
1387                 }
1388         }
1389
1390         if(b_is_new) {
1391                 account_custom_s* custom_data = (account_custom_s*)malloc(sizeof(account_custom_s));
1392
1393                 if (custom_data == NULL) {
1394                         ACCOUNT_FATAL("(%s)-(%d) MALLOC FAIL\n", __FUNCTION__, __LINE__);
1395                         return ACCOUNT_ERROR_OUT_OF_MEMORY;
1396                 }
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);
1403         }
1404
1405         return ACCOUNT_ERROR_NONE;
1406 }
1407
1408 int account_set_auth_type(account_h account, const account_auth_type_e auth_type)
1409 {
1410         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n",  __FUNCTION__, __LINE__));
1411
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;
1415         }
1416
1417         account_s *data = (account_s*)account;
1418
1419         data->auth_type = (int)auth_type;
1420
1421         return ACCOUNT_ERROR_NONE;
1422 }
1423
1424 int account_set_secret(account_h account, const account_secrecy_state_e secret)
1425 {
1426         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n",      __FUNCTION__, __LINE__));
1427
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;
1431         }
1432
1433         account_s *data = (account_s*)account;
1434
1435         data->secret = (int)secret;
1436
1437         return ACCOUNT_ERROR_NONE;
1438 }
1439
1440 int account_set_sync_support(account_h account, const account_sync_state_e sync_support)
1441 {
1442         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n",      __FUNCTION__, __LINE__));
1443
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;
1447         }
1448
1449         account_s *data = (account_s*)account;
1450
1451         data->sync_support= (int)sync_support;
1452
1453         return ACCOUNT_ERROR_NONE;
1454 }
1455
1456 int account_set_user_int(account_h account, int index, const int user_int)
1457 {
1458         if (!account) {
1459                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1460                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1461         }
1462
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;
1466         }
1467
1468         account_s *data = (account_s*)account;
1469
1470         data->user_data_int[index] = user_int;
1471
1472         return ACCOUNT_ERROR_NONE;
1473 }
1474
1475 int account_set_capability(account_h account, const char* capability_type, account_capability_state_e capability_value)
1476 {
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"));
1479
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;
1483         }
1484
1485         account_s *data = (account_s*)account;
1486
1487         GSList *iter = NULL;
1488         bool b_is_new = TRUE;
1489
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;
1493
1494                 if(!strcmp(cap_data->type, capability_type)) {
1495                         cap_data->value = capability_value;
1496                         b_is_new = FALSE;
1497                         break;
1498                 }
1499         }
1500
1501         if(b_is_new) {
1502                 account_capability_s* cap_data = (account_capability_s*)malloc(sizeof(account_capability_s));
1503
1504                 if (cap_data == NULL)
1505                         return ACCOUNT_ERROR_OUT_OF_MEMORY;
1506                 ACCOUNT_MEMSET(cap_data, 0, sizeof(account_capability_s));
1507
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);
1511         }
1512
1513         return ACCOUNT_ERROR_NONE;
1514 }
1515
1516 int account_get_user_name(account_h account, char **user_name)
1517 {
1518         if (!account) {
1519                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1520                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1521         }
1522
1523         if (!user_name) {
1524                 ACCOUNT_ERROR("(%s)-(%d) user name is NULL.\n", __FUNCTION__, __LINE__);
1525                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1526         }
1527
1528         account_s *data = (account_s*)account;
1529
1530         (*user_name) = NULL;
1531         *user_name = _account_get_text(data->user_name);
1532
1533         return ACCOUNT_ERROR_NONE;
1534 }
1535
1536 int account_get_display_name(account_h account, char **display_name)
1537 {
1538         if (!account) {
1539                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1540                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1541         }
1542
1543         if (!display_name) {
1544                 ACCOUNT_ERROR("(%s)-(%d) display name is NULL.\n", __FUNCTION__, __LINE__);
1545                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1546         }
1547
1548         account_s *data = (account_s*)account;
1549
1550         (*display_name) = NULL;
1551
1552         *display_name = _account_get_text(data->display_name);
1553
1554         return ACCOUNT_ERROR_NONE;
1555 }
1556
1557 int account_get_email_address(account_h account,char **email_address)
1558 {
1559         if (!account) {
1560                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1561                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1562         }
1563
1564         if (!email_address) {
1565                 ACCOUNT_ERROR("(%s)-(%d) email address is NULL.\n", __FUNCTION__, __LINE__);
1566                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1567         }
1568
1569         account_s *data = (account_s*)account;
1570
1571         (*email_address) = NULL;
1572
1573         *email_address = _account_get_text(data->email_address);
1574
1575         return ACCOUNT_ERROR_NONE;
1576 }
1577
1578 int  account_get_icon_path(account_h account, char **icon_path)
1579 {
1580         if (!account) {
1581                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1582                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1583         }
1584
1585         if (!icon_path) {
1586                 ACCOUNT_ERROR("(%s)-(%d) icon path is NULL.\n", __FUNCTION__, __LINE__);
1587                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1588         }
1589
1590         account_s *data = (account_s*)account;
1591
1592         (*icon_path) = NULL;
1593
1594         *icon_path = _account_get_text(data->icon_path);
1595
1596         return ACCOUNT_ERROR_NONE;
1597 }
1598
1599 int account_get_source(account_h account, char **source)
1600 {
1601         if (!account) {
1602                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1603                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1604         }
1605
1606         if (!source) {
1607                 ACCOUNT_ERROR("(%s)-(%d) source is NULL.\n", __FUNCTION__, __LINE__);
1608                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1609         }
1610
1611         account_s *data = (account_s*)account;
1612
1613         (*source) = NULL;
1614
1615         *source = _account_get_text(data->source);
1616
1617         return ACCOUNT_ERROR_NONE;
1618 }
1619
1620 int account_get_package_name(account_h account, char **package_name)
1621 {
1622         if (!account) {
1623                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1624                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1625         }
1626
1627         if (!package_name) {
1628                 ACCOUNT_ERROR("(%s)-(%d) package name is NULL.\n", __FUNCTION__, __LINE__);
1629                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1630         }
1631
1632         account_s *data = (account_s*)account;
1633
1634         (*package_name) = NULL;
1635
1636         *package_name = _account_get_text(data->package_name);
1637
1638         return ACCOUNT_ERROR_NONE;
1639 }
1640
1641 int account_get_domain_name(account_h account, char **domain_name)
1642 {
1643         if (!account) {
1644                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1645                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1646         }
1647
1648         if (!domain_name) {
1649                 ACCOUNT_ERROR("(%s)-(%d) domain name is NULL.\n", __FUNCTION__, __LINE__);
1650                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1651         }
1652
1653         account_s *data = (account_s*)account;
1654
1655         (*domain_name) = NULL;
1656
1657         *domain_name = _account_get_text(data->domain_name);
1658
1659         return ACCOUNT_ERROR_NONE;
1660 }
1661
1662 int account_get_access_token(account_h account, char **access_token)
1663 {
1664         if (!account) {
1665                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1666                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1667         }
1668
1669         if (!access_token) {
1670                 ACCOUNT_ERROR("(%s)-(%d) access token is NULL.\n", __FUNCTION__, __LINE__);
1671                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1672         }
1673
1674         account_s *data = (account_s*)account;
1675
1676         (*access_token) = NULL;
1677
1678         *access_token = _account_get_text(data->access_token);
1679
1680         return ACCOUNT_ERROR_NONE;
1681 }
1682
1683 int account_get_user_text(account_h account, int user_text_index, char **text)
1684 {
1685         if (!account) {
1686                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1687                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1688         }
1689
1690         if (!text) {
1691                 ACCOUNT_ERROR("(%s)-(%d) text is NULL.\n", __FUNCTION__, __LINE__);
1692                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1693         }
1694         ACCOUNT_RETURN_VAL((user_text_index >=0 && user_text_index < USER_TXT_CNT ), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID USER TEXT INDEX"));
1695
1696         account_s *data = (account_s*)account;
1697
1698         (*text) = NULL;
1699
1700         *text = _account_get_text(data->user_data_txt[user_text_index]);
1701
1702         return ACCOUNT_ERROR_NONE;
1703 }
1704
1705 int account_get_auth_type(account_h account, account_auth_type_e *auth_type)
1706 {
1707         if (!account) {
1708                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1709                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1710         }
1711         if (!auth_type) {
1712                 ACCOUNT_ERROR("(%s)-(%d) auth_type is NULL.\n", __FUNCTION__, __LINE__);
1713                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1714         }
1715
1716         account_s* data = (account_s*)account;
1717
1718         *auth_type = data->auth_type;
1719
1720         return ACCOUNT_ERROR_NONE;
1721 }
1722
1723 int account_get_secret(account_h account, account_secrecy_state_e *secret)
1724 {
1725         if (!account) {
1726                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1727                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1728         }
1729         if (!secret) {
1730                 ACCOUNT_ERROR("(%s)-(%d) secret is NULL.\n", __FUNCTION__, __LINE__);
1731                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1732         }
1733
1734         account_s* data = (account_s*)account;
1735
1736         *secret = data->secret;
1737
1738         return ACCOUNT_ERROR_NONE;
1739 }
1740
1741 int account_get_sync_support(account_h account, account_sync_state_e *sync_support)
1742 {
1743         if (!account) {
1744                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1745                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1746         }
1747         if (!sync_support) {
1748                 ACCOUNT_ERROR("(%s)-(%d) sync_support is NULL.\n", __FUNCTION__, __LINE__);
1749                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1750         }
1751
1752         account_s* data = (account_s*)account;
1753
1754         *sync_support = data->sync_support;
1755
1756         return ACCOUNT_ERROR_NONE;
1757 }
1758
1759 int account_get_account_id(account_h account, int *account_id)
1760 {
1761         if (!account) {
1762                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1763                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1764         }
1765         if (!account_id) {
1766                 ACCOUNT_ERROR("(%s)-(%d) account_id is NULL.\n", __FUNCTION__, __LINE__);
1767                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1768         }
1769
1770         account_s *data = (account_s*)account;
1771
1772         *account_id = data->id;
1773
1774         return ACCOUNT_ERROR_NONE;
1775 }
1776
1777 int account_get_user_int(account_h account, int user_int_index, int *integer)
1778 {
1779         if (!account) {
1780                 ACCOUNT_ERROR("(%s)-(%d) account handle is NULL.\n", __FUNCTION__, __LINE__);
1781                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1782         }
1783
1784         ACCOUNT_RETURN_VAL((user_int_index >=0 && user_int_index < USER_TXT_CNT ), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("INVALID USER TEXT INDEX"));
1785
1786         if (!integer) {
1787                 ACCOUNT_ERROR("(%s)-(%d) integer is NULL.\n", __FUNCTION__, __LINE__);
1788                 return ACCOUNT_ERROR_INVALID_PARAMETER;
1789         }
1790
1791         account_s *data = (account_s*)account;
1792
1793         *integer = data->user_data_int[user_int_index];
1794
1795         return ACCOUNT_ERROR_NONE;
1796 }
1797
1798 int account_get_capability(account_h account, const char* capability_type, account_capability_state_e* capability_value)
1799 {
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"));
1803
1804         GSList *iter;
1805         account_s *data = (account_s*)account;
1806
1807         for (iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1808                 account_capability_s *cap_data = NULL;
1809
1810                 cap_data = (account_capability_s*)iter->data;
1811
1812                 ACCOUNT_VERBOSE("account_get_capability :: type = %d, value = %d", cap_data->type, cap_data->value);
1813
1814                 if(!strcmp(capability_type, cap_data->type)) {
1815                         *capability_value = cap_data->value;
1816                         return ACCOUNT_ERROR_NONE;
1817                 }
1818         }
1819
1820         return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1821 }
1822
1823 int account_get_capability_all(account_h account, capability_cb cb_func, void *user_data)
1824 {
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"));
1827
1828         GSList *iter;
1829         account_s *data = (account_s*)account;
1830
1831         for (iter = data->capablity_list; iter != NULL; iter = g_slist_next(iter)) {
1832                 account_capability_s *cap_data = NULL;
1833
1834                 cap_data = (account_capability_s*)iter->data;
1835
1836                 ACCOUNT_VERBOSE("account_get_capability :: type = %d, value = %d", cap_data->type, cap_data->value);
1837
1838                 cb_func(cap_data->type, cap_data->value, user_data);
1839         }
1840
1841         return ACCOUNT_ERROR_NONE;
1842 }
1843
1844 int account_get_custom(account_h account, const char* key, char** value)
1845 {
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"));
1849
1850         GSList *iter;
1851         account_s *data = (account_s*)account;
1852
1853         for (iter = data->custom_list; iter != NULL; iter = g_slist_next(iter)) {
1854                 account_custom_s *custom_data = NULL;
1855
1856                 custom_data = (account_custom_s*)iter->data;
1857
1858                 ACCOUNT_VERBOSE("account_get_custom ::  key = %s, value = %s", custom_data->key, custom_data->value);
1859
1860                 if(!strcmp(key, custom_data->key)) {
1861                         (*value) = NULL;
1862                         *value = _account_get_text(custom_data->value);
1863                         return ACCOUNT_ERROR_NONE;
1864                 }
1865         }
1866
1867         ACCOUNT_INFO("key is not found %s", key);
1868
1869         return ACCOUNT_ERROR_RECORD_NOT_FOUND;
1870 }
1871
1872 int account_get_custom_all(account_h account, account_custom_cb cb_func, void* user_data)
1873 {
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"));
1876
1877         GSList *iter;
1878         account_s *data = (account_s*)account;
1879
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;
1883
1884                 custom_data = (account_custom_s*)iter->data;
1885
1886                 ACCOUNT_VERBOSE("account_get_custom ::  key = %s, value = %s", custom_data->key, custom_data->value);
1887
1888                 cb_ret = cb_func(custom_data->key, custom_data->value, user_data);
1889                 if(!cb_ret) {
1890                         ACCOUNT_INFO("account_get_custom_all callback func ret = %d", cb_ret);
1891                         break;
1892         }
1893         }
1894
1895         return ACCOUNT_ERROR_NONE;
1896 }
1897
1898 int account_query_capability_by_account_id(capability_cb cb_func, int account_id, void *user_data )
1899 {
1900         int                     error_code = ACCOUNT_ERROR_NONE;
1901         account_stmt    hstmt = NULL;
1902         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1903         int                     rc = 0;
1904         int ret = ACCOUNT_ERROR_NONE;
1905
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."));
1909
1910         ret = _do_account_owner_existance_check();
1911         if (ret != ACCOUNT_ERROR_NONE) {
1912                 ACCOUNT_ERROR("_do_account_owner_existance_check Failed !!!\n");
1913         }
1914
1915         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1916
1917         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
1918         hstmt = _account_prepare_query(query);
1919
1920         rc = _account_query_step(hstmt);
1921         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1922
1923         account_capability_s* capability_record = NULL;
1924
1925         while (rc == SQLITE_ROW) {
1926                 capability_record = (account_capability_s*) malloc(sizeof(account_capability_s));
1927
1928                 if (capability_record == NULL) {
1929                         ACCOUNT_FATAL("malloc Failed");
1930                         break;
1931                 }
1932
1933                 ACCOUNT_MEMSET(capability_record, 0x00, sizeof(account_capability_s));
1934
1935                 _account_convert_column_to_capability(hstmt, capability_record);
1936
1937                 cb_func(capability_record->type, capability_record->value, user_data);
1938
1939                 _account_free_capability_items(capability_record);
1940                 _ACCOUNT_FREE(capability_record);
1941
1942                 rc = _account_query_step(hstmt);
1943         }
1944
1945         _account_query_finalize(hstmt);
1946         hstmt = NULL;
1947
1948         error_code = ACCOUNT_ERROR_NONE;
1949
1950 CATCH:
1951         if (hstmt != NULL) {
1952                 _account_query_finalize(hstmt);
1953                 hstmt = NULL;
1954         }
1955
1956         pthread_mutex_unlock(&account_mutex);
1957         return error_code;
1958 }
1959
1960 #define URUSA_TEST
1961
1962 #ifdef URUSA_TEST
1963 static int _account_compare_old_record(account_s *new_account, int account_id)
1964 {
1965         int                             error_code = ACCOUNT_ERROR_NONE;
1966         account_stmt    hstmt = NULL;
1967         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
1968         int                             rc = 0;
1969         account_s *old_account = NULL;
1970
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."));
1974
1975         old_account = (account_s*)calloc(1, sizeof(account_s));
1976
1977         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
1978
1979         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
1980         hstmt = _account_prepare_query(query);
1981
1982         rc = _account_query_step(hstmt);
1983         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
1984
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);
1989         }
1990
1991         _account_query_finalize(hstmt);
1992         hstmt = NULL;
1993
1994         // get capability
1995         error_code = account_query_capability_by_account_id(_account_get_capability_text_cb, old_account->id, (void*)old_account);
1996
1997         // get custom text
1998         error_code = _account_query_custom_by_account_id(_account_get_custom_text_cb, old_account->id, (void*)old_account);
1999
2000
2001         // compare
2002
2003         //user name
2004         if(!new_account->user_name) {
2005                 if(old_account->user_name)
2006                         new_account->user_name = _account_get_text(old_account->user_name);
2007         }
2008
2009         // display 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);
2013         }
2014
2015         // email address
2016         if(!new_account->email_address) {
2017                 if(old_account->email_address)
2018                         new_account->email_address = _account_get_text(old_account->email_address);
2019         }
2020
2021         // icon path
2022         if(!new_account->icon_path) {
2023                 if(old_account->icon_path)
2024                         new_account->icon_path = _account_get_text(old_account->icon_path);
2025         }
2026
2027         // source
2028         if(!new_account->source) {
2029                 if(old_account->source)
2030                         new_account->source = _account_get_text(old_account->source);
2031         }
2032
2033         // package name
2034         if(!new_account->package_name) {
2035                 if(old_account->package_name)
2036                         new_account->package_name = _account_get_text(old_account->package_name);
2037         }
2038
2039         // access token
2040         if(!new_account->access_token) {
2041                 if(old_account->access_token)
2042                         new_account->access_token = _account_get_text(old_account->access_token);
2043         }
2044
2045         // user text
2046         int i;
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]);
2051                 }
2052         }
2053
2054         // auth type
2055         if(new_account->auth_type == ACCOUNT_AUTH_TYPE_INVALID) {
2056                 new_account->auth_type = old_account->auth_type;
2057         }
2058
2059         //secret
2060         if(new_account->secret== ACCOUNT_SECRECY_INVALID) {
2061                 new_account->secret = old_account->secret;
2062         }
2063
2064         // sync support
2065         if(new_account->sync_support == ACCOUNT_SYNC_INVALID) {
2066                 new_account->sync_support = old_account->sync_support;
2067         }
2068
2069         // user int
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];
2073                 }
2074         }
2075
2076         // capability
2077
2078         // user custom table
2079
2080
2081
2082         CATCH:
2083                 if (hstmt != NULL) {
2084                         _account_query_finalize(hstmt);
2085                         hstmt = NULL;
2086                 }
2087
2088                 if (old_account) {
2089                         _account_free_account_items(old_account);
2090                         _ACCOUNT_FREE(old_account);
2091                 }
2092
2093         return ACCOUNT_ERROR_NONE;
2094 }
2095 #endif
2096
2097 static int _account_update_account(account_s *account, int account_id)
2098 {
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;
2103
2104
2105 #ifdef URUSA_TEST
2106         _account_compare_old_record(account, account_id);
2107 #endif
2108
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;
2112         }
2113
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;
2117         }
2118
2119         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2120
2121         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id = %d ", ACCOUNT_TABLE, account_id);
2122
2123         count = _account_get_record_count(query);
2124         if (count <= 0) {
2125                 ACCOUNT_WARNING(" Account record not found, count = %d\n", count);
2126                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2127         }
2128
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);
2134
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()));
2137
2138         binding_count = _account_convert_account_to_sql(account, hstmt, query);
2139         _account_query_bind_int(hstmt, binding_count++, account_id);
2140
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());
2144         }
2145
2146         _account_query_finalize(hstmt);
2147         hstmt = NULL;
2148
2149         /*update capability*/
2150         error_code = _account_update_capability(account, account_id);
2151
2152         /* update custom */
2153         error_code = _account_update_custom(account, account_id);
2154
2155         return error_code;
2156 }
2157
2158 int account_update_to_db_by_id(const account_h account, int account_id)
2159 {
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;
2165
2166         pthread_mutex_lock(&account_mutex);
2167
2168         error_code = _account_update_account(data, account_id);
2169
2170         if(error_code != ACCOUNT_ERROR_NONE) {
2171                 pthread_mutex_unlock(&account_mutex);
2172                 return error_code;
2173         }
2174
2175         pthread_mutex_unlock(&account_mutex);
2176         _account_insert_delete_update_notification_send(ACCOUNT_NOTI_NAME_UPDATE);
2177
2178         return ACCOUNT_ERROR_NONE;
2179 }
2180
2181 int account_update_to_db_by_user_name(account_h account, const char *user_name, const char *package_name)
2182 {
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."));
2187
2188         int     error_code = ACCOUNT_ERROR_NONE;
2189         account_s *data = (account_s*)account;
2190
2191         pthread_mutex_lock(&account_mutex);
2192
2193         error_code = _account_update_account_by_user_name(data, (char*)user_name, (char*)package_name);
2194
2195         pthread_mutex_unlock(&account_mutex);
2196         _account_insert_delete_update_notification_send(ACCOUNT_NOTI_NAME_UPDATE);
2197
2198         return ACCOUNT_ERROR_NONE;
2199 }
2200
2201 int account_foreach_account_from_db(account_cb callback, void *user_data)
2202 {
2203         int                     error_code = ACCOUNT_ERROR_NONE;
2204         account_stmt    hstmt = NULL;
2205         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2206         int                     rc = 0;
2207         GList                   *account_list = NULL;
2208         int ret = ACCOUNT_ERROR_NONE;
2209
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."));
2212
2213         ret = _do_account_owner_existance_check();
2214         if (ret != ACCOUNT_ERROR_NONE) {
2215                 ACCOUNT_WARNING("_do_account_owner_existance_check Failed !!!\n");
2216         }
2217
2218         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2219
2220         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TABLE);
2221         hstmt = _account_prepare_query(query);
2222
2223         rc = _account_query_step(hstmt);
2224
2225         account_s *account_record = NULL;
2226
2227         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2228
2229         int k=0;
2230         while(rc == SQLITE_ROW) {
2231                 account_record = (account_s*) malloc(sizeof(account_s));
2232
2233                 if (account_record == NULL) {
2234                         ACCOUNT_FATAL("malloc Failed");
2235                         break;
2236                 }
2237
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);
2242                 k++;
2243         }
2244
2245         _account_query_finalize(hstmt);
2246         hstmt = NULL;
2247
2248         GList* iter;
2249         k = 0;
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);
2256                 k++;
2257         }
2258
2259         error_code = ACCOUNT_ERROR_NONE;
2260
2261 CATCH:
2262         if (hstmt != NULL) {
2263                 _account_query_finalize(hstmt);
2264                 hstmt = NULL;
2265         }
2266         if (account_list) {
2267                 _account_glist_free(account_list);
2268                 account_list = NULL;
2269         }
2270
2271         return error_code;
2272 }
2273
2274 int account_update_sync_status_by_id(int account_db_id, const account_sync_state_e sync_status)
2275 {
2276         int                             error_code = ACCOUNT_ERROR_NONE;
2277         account_stmt    hstmt = NULL;
2278         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2279         int                             rc = 0;
2280         int count =1;
2281
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;
2287         }
2288
2289         pthread_mutex_lock(&account_mutex);
2290
2291         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2292
2293         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_db_id);
2294
2295         rc = _account_get_record_count(query);
2296
2297         if (rc <= 0) {
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;
2301         }
2302
2303         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2304
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);
2307
2308         _account_query_bind_int(hstmt, count, (int)sync_status);
2309
2310         rc = _account_query_step(hstmt);
2311
2312         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_DB_FAILED,
2313                                 ("account_db_query_step() failed(%d, %s)", rc, _account_db_err_msg()));
2314
2315         _account_query_finalize(hstmt);
2316
2317         _account_insert_delete_update_notification_send(ACCOUNT_NOTI_NAME_SYNC_UPDATE);
2318
2319         hstmt = NULL;
2320         error_code = ACCOUNT_ERROR_NONE;
2321
2322 CATCH:
2323         if (hstmt != NULL) {
2324                 _account_query_finalize(hstmt);
2325                 hstmt = NULL;
2326         }
2327
2328         pthread_mutex_unlock(&account_mutex);
2329         return error_code;
2330 }
2331
2332 int account_query_account_by_account_id(int account_db_id, account_h *account)
2333 {
2334         int                             error_code = ACCOUNT_ERROR_NONE;
2335         account_stmt    hstmt = NULL;
2336         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2337         int                             rc = 0;
2338         int ret = ACCOUNT_ERROR_NONE;
2339
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."));
2343
2344         ret = _do_account_owner_existance_check();
2345         if (ret != ACCOUNT_ERROR_NONE) {
2346                 ACCOUNT_WARNING("_do_account_owner_existance_check Failed !!!\n");
2347         }
2348
2349         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2350
2351         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_db_id);
2352         hstmt = _account_prepare_query(query);
2353
2354         rc = _account_query_step(hstmt);
2355         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2356
2357         account_s *account_record = (account_s *)(*account);
2358
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);
2363         }
2364
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);
2368
2369         hstmt = NULL;
2370         error_code = ACCOUNT_ERROR_NONE;
2371
2372 CATCH:
2373         if (hstmt != NULL) {
2374                 _account_query_finalize(hstmt);
2375                 hstmt = NULL;
2376         }
2377
2378         pthread_mutex_unlock(&account_mutex);
2379         return error_code;
2380 }
2381
2382 int account_query_account_by_user_name(account_cb cb_func, const char *user_name, void *user_data)
2383 {
2384         int                             error_code = ACCOUNT_ERROR_NONE;
2385         account_stmt    hstmt = NULL;
2386         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2387         int                             rc = 0;
2388
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"));
2391
2392
2393         int ret = ACCOUNT_ERROR_NONE;
2394
2395
2396         ret = _do_account_owner_existance_check();
2397         if (ret != ACCOUNT_ERROR_NONE) {
2398                 ACCOUNT_WARNING("_do_account_owner_existance_check Failed !!!\n");
2399         }
2400
2401         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2402
2403         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ?", ACCOUNT_TABLE);
2404
2405         hstmt = _account_prepare_query(query);
2406
2407         int binding_count = 1;
2408         _account_query_bind_text(hstmt, binding_count++, user_name);
2409
2410         rc = _account_query_step(hstmt);
2411
2412         account_s *account_head = NULL;
2413
2414         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2415
2416         int tmp = 0;
2417
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);
2423                         hstmt = NULL;
2424                 }
2425                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
2426         }
2427         ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2428
2429         while (rc == SQLITE_ROW) {
2430                 account_s* account_record = NULL;
2431
2432                 account_record = (account_s*) malloc(sizeof(account_s));
2433
2434                 if (account_record == NULL) {
2435                         ACCOUNT_FATAL("malloc Failed");
2436                         break;
2437                 }
2438                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2439
2440                 _account_convert_column_to_account(hstmt, account_record);
2441
2442                 account_head->account_list = g_list_append(account_head->account_list, account_record);
2443
2444                 rc = _account_query_step(hstmt);
2445                 tmp++;
2446                 ACCOUNT_DEBUG("DETECTED COUNT %d\n", tmp);
2447         }
2448
2449         _account_query_finalize(hstmt);
2450         hstmt = NULL;
2451
2452         GList *iter;
2453
2454
2455         tmp = g_list_length(account_head->account_list);
2456         ACCOUNT_VERBOSE("GLIST LEN %d\n", tmp);
2457
2458         for (iter = account_head->account_list; iter != NULL; iter = g_list_next(iter)) {
2459                 account_h account;
2460                 account = (account_h)iter->data;
2461
2462                 account_s *testaccount = (account_s*)account;
2463
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);
2476
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);
2479
2480                 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
2481
2482                 cb_func(account, user_data);
2483
2484         }
2485
2486         error_code = ACCOUNT_ERROR_NONE;
2487
2488 CATCH:
2489         if (hstmt != NULL) {
2490                 _account_query_finalize(hstmt);
2491                 hstmt = NULL;
2492         }
2493         if (account_head) {
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);
2498                 }
2499                 _ACCOUNT_FREE(account_head);
2500         }
2501
2502         pthread_mutex_unlock(&account_mutex);
2503         return error_code;
2504 }
2505
2506 int account_query_account_by_capability(account_cb cb_func, const char* capability_type, account_capability_state_e capability_value, void* user_data)
2507 {
2508         int                     error_code = ACCOUNT_ERROR_NONE;
2509         account_stmt    hstmt = NULL;
2510         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2511         int                     rc = 0;
2512         int ret = ACCOUNT_ERROR_NONE;
2513
2514         ACCOUNT_RETURN_VAL((capability_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("capability_type IS NULL"));
2515
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;
2519         }
2520
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."));
2523
2524         ret = _do_account_owner_existance_check();
2525         if (ret != ACCOUNT_ERROR_NONE) {
2526                 ACCOUNT_WARNING("_do_account_owner_existance_check Failed !!!\n");
2527         }
2528
2529         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2530
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);
2532
2533         hstmt = _account_prepare_query(query);
2534
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);
2538
2539         rc = _account_query_step(hstmt);
2540
2541         account_s* account_head = NULL;
2542
2543         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2544
2545         int tmp = 0;
2546
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);
2552                         hstmt = NULL;
2553                 }
2554                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
2555         }
2556         ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2557
2558         while (rc == SQLITE_ROW) {
2559                 account_s* account_record = NULL;
2560
2561                 account_record = (account_s*) malloc(sizeof(account_s));
2562
2563                 if (account_record == NULL) {
2564                         ACCOUNT_FATAL("malloc Failed");
2565                         break;
2566                 }
2567                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2568
2569                 _account_convert_column_to_account(hstmt, account_record);
2570
2571                 account_head->account_list = g_list_append(account_head->account_list, account_record);
2572
2573                 rc = _account_query_step(hstmt);
2574                 tmp++;
2575                 ACCOUNT_VERBOSE("DETECTED COUNT %d\n", tmp);
2576         }
2577
2578         _account_query_finalize(hstmt);
2579         hstmt = NULL;
2580
2581         GList *iter;
2582
2583
2584         tmp = g_list_length(account_head->account_list);
2585         ACCOUNT_VERBOSE("GLIST LEN %d\n", tmp);
2586
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;
2590
2591
2592                 account_s* testaccount = (account_s*)account;
2593
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);
2606
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);
2609
2610                 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
2611
2612                 cb_func(account, user_data);
2613
2614         }
2615
2616
2617         error_code = ACCOUNT_ERROR_NONE;
2618
2619 CATCH:
2620         if (hstmt != NULL) {
2621                 _account_query_finalize(hstmt);
2622                 hstmt = NULL;
2623         }
2624         if (account_head) {
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);
2629                 }
2630                 _ACCOUNT_FREE(account_head);
2631         }
2632
2633         pthread_mutex_unlock(&account_mutex);
2634         return error_code;
2635 }
2636
2637 int account_query_account_by_package_name(account_cb cb_func, const char* package_name, void* user_data)
2638 {
2639         int                     error_code = ACCOUNT_ERROR_NONE;
2640         account_stmt    hstmt = NULL;
2641         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2642         int                     rc = 0;
2643         int ret = ACCOUNT_ERROR_NONE;
2644
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."));
2648
2649         ret = _do_account_owner_existance_check();
2650         if (ret != ACCOUNT_ERROR_NONE) {
2651                 ACCOUNT_ERROR("_do_account_owner_existance_check Failed !!!\n");
2652         }
2653
2654         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2655
2656         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE package_name=?", ACCOUNT_TABLE);
2657
2658         hstmt = _account_prepare_query(query);
2659
2660         int binding_count = 1;
2661         _account_query_bind_text(hstmt, binding_count++, package_name);
2662
2663         rc = _account_query_step(hstmt);
2664
2665         account_s* account_head = NULL;
2666
2667         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2668
2669         int tmp = 0;
2670
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);
2676                         hstmt = NULL;
2677                 }
2678                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
2679         }
2680         ACCOUNT_MEMSET(account_head, 0x00, sizeof(account_s));
2681
2682         while (rc == SQLITE_ROW) {
2683                 account_s* account_record = NULL;
2684
2685                 account_record = (account_s*) malloc(sizeof(account_s));
2686
2687                 if (account_record == NULL) {
2688                         ACCOUNT_FATAL("malloc Failed");
2689                         break;
2690                 }
2691                 ACCOUNT_MEMSET(account_record, 0x00, sizeof(account_s));
2692
2693                 _account_convert_column_to_account(hstmt, account_record);
2694
2695                 account_head->account_list = g_list_append(account_head->account_list, account_record);
2696
2697                 rc = _account_query_step(hstmt);
2698                 tmp++;
2699                 ACCOUNT_VERBOSE("DETECTED COUNT %d\n", tmp);
2700         }
2701
2702         _account_query_finalize(hstmt);
2703         hstmt = NULL;
2704
2705         GList *iter;
2706
2707         tmp = g_list_length(account_head->account_list);
2708         ACCOUNT_DEBUG("GLIST LEN %d\n", tmp);
2709
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;
2713
2714                 account_s* testaccount = (account_s*)account;
2715
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);
2728
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);
2731
2732                 ACCOUNT_VERBOSE("capability_list address = %p", testaccount->capablity_list);
2733
2734                 cb_func(account, user_data);
2735
2736         }
2737
2738         error_code = ACCOUNT_ERROR_NONE;
2739
2740 CATCH:
2741         if (hstmt != NULL) {
2742                 _account_query_finalize(hstmt);
2743                 hstmt = NULL;
2744         }
2745         if (account_head) {
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);
2750                 }
2751                 _ACCOUNT_FREE(account_head);
2752         }
2753
2754         pthread_mutex_unlock(&account_mutex);
2755         return error_code;
2756 }
2757
2758 int account_delete(int account_id)
2759 {
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;
2766
2767         ACCOUNT_RETURN_VAL((g_hAccountDB != NULL), {}, ACCOUNT_ERROR_DB_NOT_OPENED, ("The database isn't connected."));
2768
2769         /* Check requested ID to delete */
2770         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE _id=%d", ACCOUNT_TABLE, account_id);
2771
2772         count = _account_get_record_count(query);
2773         if (count <= 0) {
2774                 ACCOUNT_ERROR("account id(%d) is not exist. count(%d)\n", account_id, count);
2775                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
2776         }
2777
2778         /* transaction control required*/
2779         ret_transaction = _account_begin_transaction();
2780
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;
2785         }
2786
2787         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
2788         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE account_id = %d", CAPABILITY_TABLE, account_id);
2789
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()));
2793
2794         rc = _account_query_step(hstmt);
2795         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2796
2797         _account_query_finalize(hstmt);
2798         hstmt = NULL;
2799
2800         ACCOUNT_MEMSET(query, 0, sizeof(query));
2801
2802         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE _id = %d", ACCOUNT_TABLE, account_id);
2803
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()));
2807
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));
2810
2811         _account_query_finalize(hstmt);
2812         hstmt = NULL;
2813
2814         /* delete custom data */
2815         ACCOUNT_MEMSET(query, 0, sizeof(query));
2816
2817         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
2818
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()));
2822
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));
2825
2826         _account_query_finalize(hstmt);
2827
2828         is_success = TRUE;
2829
2830 CATCH:
2831         if (hstmt != NULL) {
2832                 _account_query_finalize(hstmt);
2833                 hstmt = NULL;
2834         }
2835
2836         ret_transaction = _account_end_transaction(is_success);
2837
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);
2840         } else {
2841                 if (is_success == true)
2842                         _account_insert_delete_update_notification_send(ACCOUNT_NOTI_NAME_DELETE);
2843         }
2844
2845         pthread_mutex_unlock(&account_mutex);
2846
2847         return error_code;
2848
2849 }
2850
2851 int account_delete_from_db_by_id(int account_db_id)
2852 {
2853         ACCOUNT_RETURN_VAL((account_db_id > 0), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT ID IS LESS THAN ZERO."));
2854
2855         return account_delete(account_db_id);
2856 }
2857
2858 static int _account_query_account_by_username_and_package(const char* username, const char* package_name, account_h *account)
2859 {
2860         int                             error_code = ACCOUNT_ERROR_NONE;
2861         account_stmt    hstmt = NULL;
2862         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2863         int                             rc = 0;
2864         int                     ret = ACCOUNT_ERROR_NONE;
2865         int                             binding_count = 1;
2866
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."));
2871
2872         ret = _do_account_owner_existance_check();
2873         if (ret != ACCOUNT_ERROR_NONE) {
2874                 ACCOUNT_WARNING("_do_account_owner_existance_check Failed !!!\n");
2875         }
2876
2877         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
2878
2879         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
2880         hstmt = _account_prepare_query(query);
2881
2882         _account_query_bind_text(hstmt, binding_count++, username);
2883         _account_query_bind_text(hstmt, binding_count++, package_name);
2884
2885         rc = _account_query_step(hstmt);
2886         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2887
2888         account_s *account_record = (account_s *)(*account);
2889
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);
2894         }
2895
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);
2899
2900         hstmt = NULL;
2901         error_code = ACCOUNT_ERROR_NONE;
2902
2903 CATCH:
2904         if (hstmt != NULL) {
2905                 _account_query_finalize(hstmt);
2906                 hstmt = NULL;
2907         }
2908
2909         pthread_mutex_unlock(&account_mutex);
2910         return error_code;
2911 }
2912
2913 int account_delete_from_db_by_user_name(char *user_name, char *package_name)
2914 {
2915         int                     error_code = ACCOUNT_ERROR_NONE;
2916         account_stmt    hstmt = NULL;
2917         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
2918         int                     rc = 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;
2924
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."));
2928
2929         rc = account_create(&account);
2930         rc = _account_query_account_by_username_and_package(user_name, package_name, &account);
2931
2932         rc = account_get_account_id(account, &account_id);
2933
2934         rc = account_destroy(account);
2935
2936         /* transaction control required*/
2937         ret_transaction = _account_begin_transaction();
2938
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;
2943         }
2944
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);
2948
2949         hstmt = _account_prepare_query(query);
2950
2951         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
2952                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
2953
2954         _account_query_bind_int(hstmt, binding_count++, account_id);
2955
2956         rc = _account_query_step(hstmt);
2957         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2958
2959         _account_query_finalize(hstmt);
2960         hstmt = NULL;
2961
2962         /* delete capability */
2963         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", CAPABILITY_TABLE);
2964
2965         hstmt = _account_prepare_query(query);
2966
2967         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
2968                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
2969
2970         binding_count = 1;
2971         _account_query_bind_text(hstmt, binding_count++, user_name);
2972         _account_query_bind_text(hstmt, binding_count++, package_name);
2973
2974         rc = _account_query_step(hstmt);
2975         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
2976
2977         _account_query_finalize(hstmt);
2978         hstmt = NULL;
2979
2980         ACCOUNT_MEMSET(query, 0, sizeof(query));
2981
2982         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE user_name = ? and package_name = ?", ACCOUNT_TABLE);
2983
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()));
2987
2988         binding_count = 1;
2989         _account_query_bind_text(hstmt, binding_count++, user_name);
2990         _account_query_bind_text(hstmt, binding_count++, package_name);
2991
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));
2994
2995         _account_query_finalize(hstmt);
2996         is_success = TRUE;
2997
2998         hstmt = NULL;
2999
3000 CATCH:
3001         if (hstmt != NULL) {
3002                 _account_query_finalize(hstmt);
3003                 hstmt = NULL;
3004         }
3005
3006         ret_transaction = _account_end_transaction(is_success);
3007
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);
3010         } else {
3011                 if (is_success == true)
3012                         _account_insert_delete_update_notification_send(ACCOUNT_NOTI_NAME_DELETE);
3013         }
3014
3015         pthread_mutex_unlock(&account_mutex);
3016
3017         return error_code;
3018 }
3019
3020 int account_delete_from_db_by_package_name(char *package_name)
3021 {
3022         int                     error_code = ACCOUNT_ERROR_NONE;
3023         account_stmt    hstmt = NULL;
3024         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3025         int                     rc = 0;
3026         int                     ret_transaction = 0;
3027         bool                    is_success = FALSE;
3028         int                     binding_count = 1;
3029
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."));
3032
3033         /* transaction control required*/
3034         ret_transaction = _account_begin_transaction();
3035
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;
3040         }
3041
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);
3045
3046         hstmt = _account_prepare_query(query);
3047
3048         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3049                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3050
3051         binding_count = 1;
3052         _account_query_bind_text(hstmt, binding_count++, package_name);
3053
3054         rc = _account_query_step(hstmt);
3055         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3056
3057         _account_query_finalize(hstmt);
3058         hstmt = NULL;
3059
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);
3063
3064         hstmt = _account_prepare_query(query);
3065
3066         ACCOUNT_CATCH_ERROR(hstmt != NULL, {}, ACCOUNT_ERROR_DB_FAILED,
3067                         ("_account_svc_query_prepare(%s) failed(%s).\n", query, _account_db_err_msg()));
3068
3069         binding_count = 1;
3070         _account_query_bind_text(hstmt, binding_count++, package_name);
3071
3072         rc = _account_query_step(hstmt);
3073         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3074
3075         _account_query_finalize(hstmt);
3076         hstmt = NULL;
3077
3078         /* delete account table */
3079         ACCOUNT_MEMSET(query, 0, sizeof(query));
3080
3081         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE package_name = ?", ACCOUNT_TABLE);
3082
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()));
3086
3087         binding_count = 1;
3088         _account_query_bind_text(hstmt, binding_count++, package_name);
3089
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));
3092
3093         _account_query_finalize(hstmt);
3094         is_success = TRUE;
3095
3096         hstmt = NULL;
3097
3098 CATCH:
3099         if (hstmt != NULL) {
3100                 _account_query_finalize(hstmt);
3101                 hstmt = NULL;
3102         }
3103
3104         ret_transaction = _account_end_transaction(is_success);
3105
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);
3108         } else {
3109                 if (is_success == true)
3110                         _account_insert_delete_update_notification_send(ACCOUNT_NOTI_NAME_DELETE);
3111         }
3112
3113         pthread_mutex_unlock(&account_mutex);
3114
3115         return error_code;
3116 }
3117
3118 int account_get_total_count_from_db(int *count)
3119 {
3120         if (!count) {
3121                 ACCOUNT_ERROR("(%s)-(%d) count is NULL.\n", __FUNCTION__, __LINE__);
3122                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3123         }
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);
3128
3129         ret = _do_account_owner_existance_check();
3130         if (ret != ACCOUNT_ERROR_NONE) {
3131                 ACCOUNT_WARNING("_do_account_owner_existance_check Failed !!!\n");
3132         }
3133
3134         *count = _account_get_record_count(query);
3135         int rc = -1;
3136         int ncount = 0;
3137         account_stmt pStmt = NULL;
3138
3139         assert(NULL != g_hAccountDB);
3140         rc = sqlite3_prepare_v2(g_hAccountDB, query, strlen(query), &pStmt, NULL);
3141
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;
3147         }
3148
3149         ncount = sqlite3_column_int(pStmt, 0);
3150
3151         *count = ncount;
3152
3153         ACCOUNT_VERBOSE("Number of account : %d", ncount);
3154         sqlite3_finalize(pStmt);
3155
3156         if (ncount < 0) {
3157                 ACCOUNT_ERROR("[ERROR] Number of account : %d, End", ncount);
3158                 return ACCOUNT_ERROR_DB_FAILED;
3159         }
3160
3161         return ACCOUNT_ERROR_NONE;
3162 }
3163
3164 int account_destroy(account_h account)
3165 {
3166         account_s *data = (account_s*)account;
3167
3168         ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account handle is null!"));
3169
3170         ACCOUNT_INFO("destroy handle=%p\n", data);
3171
3172         _account_free_account_items(data);
3173         _ACCOUNT_FREE(data);
3174
3175         return ACCOUNT_ERROR_NONE;
3176 }
3177
3178 static int _account_type_free_label_items(label_s *data)
3179 {
3180         _ACCOUNT_FREE(data->app_id);
3181         _ACCOUNT_FREE(data->label);
3182         _ACCOUNT_FREE(data->locale);
3183
3184         return ACCOUNT_ERROR_NONE;
3185 }
3186
3187 static int _account_type_gslist_free(GSList* list)
3188 {
3189         ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("GSlist is NULL"));
3190
3191         GSList* iter;
3192
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);
3197         }
3198
3199         g_slist_free(list);
3200         list = NULL;
3201
3202         return ACCOUNT_ERROR_NONE;
3203 }
3204
3205 static int _account_type_item_free(account_type_s *data)
3206 {
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);
3211
3212         return ACCOUNT_ERROR_NONE;
3213 }
3214
3215 static int _account_type_glist_free(GList* list)
3216 {
3217         ACCOUNT_RETURN_VAL((list != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Glist is NULL"));
3218
3219         GList* iter;
3220
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);
3225         }
3226
3227         g_list_free(list);
3228         list = NULL;
3229
3230         return ACCOUNT_ERROR_NONE;
3231 }
3232
3233 static int _account_type_free_account_type_items(account_type_s *data)
3234 {
3235         _account_type_item_free(data);
3236
3237         _account_type_gslist_free(data->label_list);
3238         _account_type_glist_free(data->account_type_list);
3239
3240         return ACCOUNT_ERROR_NONE;
3241 }
3242
3243 int account_type_create(account_type_h *account_type)
3244 {
3245         if (!account_type) {
3246                 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
3247                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3248         }
3249
3250         account_type_s *data = (account_type_s*)malloc(sizeof(account_type_s));
3251
3252         if (data == NULL) {
3253                 ACCOUNT_ERROR("Memory Allocation Failed");
3254                 return ACCOUNT_ERROR_OUT_OF_MEMORY;
3255         }
3256
3257         ACCOUNT_MEMSET(data, 0, sizeof(account_type_s));
3258
3259         ACCOUNT_VERBOSE("create handle=%p\n", *account_type);
3260
3261         *account_type = (account_type_h)data;
3262
3263         return ACCOUNT_ERROR_NONE;
3264 }
3265
3266 int account_type_destroy(account_type_h account_type)
3267 {
3268         account_type_s *data = (account_type_s*)account_type;
3269
3270         ACCOUNT_RETURN_VAL((data != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("Account type handle is null!"));
3271
3272         ACCOUNT_VERBOSE("destroy handle=%p\n", data);
3273
3274         _account_type_free_account_type_items(data);
3275         _ACCOUNT_FREE(data);
3276
3277         return ACCOUNT_ERROR_NONE;
3278 }
3279
3280 //app_id mandatory field
3281 int account_type_set_app_id(account_type_h account_type, const char *app_id)
3282 {
3283         if (!account_type) {
3284                 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3285                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3286         }
3287
3288         if (!app_id) {
3289                 ACCOUNT_ERROR("(%s)-(%d) app_id is NULL.\n", __FUNCTION__, __LINE__);
3290                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3291         }
3292
3293         account_type_s *data = (account_type_s*)account_type;
3294
3295         _ACCOUNT_FREE(data->app_id);
3296         data->app_id = _account_get_text(app_id);
3297
3298         ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->app_id, app_id);
3299         return ACCOUNT_ERROR_NONE;
3300 }
3301
3302 //service_provider_id mandatory field
3303 int account_type_set_service_provider_id(account_type_h account_type, const char *service_provider_id)
3304 {
3305         if (!account_type) {
3306                 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3307                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3308         }
3309
3310         if (!service_provider_id) {
3311                 ACCOUNT_ERROR("(%s)-(%d) service_provider_id is NULL.\n", __FUNCTION__, __LINE__);
3312                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3313         }
3314
3315         account_type_s *data = (account_type_s*)account_type;
3316
3317         _ACCOUNT_FREE(data->service_provider_id);
3318         data->service_provider_id = _account_get_text(service_provider_id);
3319
3320         ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->service_provider_id, service_provider_id);
3321         return ACCOUNT_ERROR_NONE;
3322 }
3323
3324 int account_type_set_icon_path(account_type_h account_type, const char *icon_path)
3325 {
3326         if (!account_type) {
3327                 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3328                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3329         }
3330
3331         if (!icon_path) {
3332                 ACCOUNT_ERROR("(%s)-(%d) icon_path is NULL.\n", __FUNCTION__, __LINE__);
3333                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3334         }
3335
3336         account_type_s *data = (account_type_s*)account_type;
3337
3338         _ACCOUNT_FREE(data->icon_path);
3339         data->icon_path = _account_get_text(icon_path);
3340
3341         ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->icon_path, icon_path);
3342         return ACCOUNT_ERROR_NONE;
3343 }
3344
3345 int account_type_set_small_icon_path(account_type_h account_type, const char *small_icon_path)
3346 {
3347         if (!account_type) {
3348                 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3349                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3350         }
3351
3352         if (!small_icon_path) {
3353                 ACCOUNT_ERROR("(%s)-(%d) small_icon_path is NULL.\n", __FUNCTION__, __LINE__);
3354                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3355         }
3356
3357         account_type_s *data = (account_type_s*)account_type;
3358
3359         _ACCOUNT_FREE(data->small_icon_path);
3360         data->small_icon_path = _account_get_text(small_icon_path);
3361
3362         ACCOUNT_VERBOSE("(%s)-(%d) %s, %s\n", __FUNCTION__, __LINE__ , data->small_icon_path, small_icon_path);
3363         return ACCOUNT_ERROR_NONE;
3364 }
3365
3366 int account_type_set_multiple_account_support(account_type_h account_type, const bool multiple_account_support)
3367 {
3368         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("(%s)-(%d) account handle is NULL.\n",  __FUNCTION__, __LINE__));
3369
3370         account_type_s *data = (account_type_s*)account_type;
3371
3372         data->multiple_account_support = multiple_account_support;
3373
3374         return ACCOUNT_ERROR_NONE;
3375 }
3376
3377 // unset?
3378 int account_type_set_label(account_type_h account_type, const char* label, const char* locale)
3379 {
3380         if (!account_type) {
3381                 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3382                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3383         }
3384
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;
3388         }
3389
3390         account_type_s *data = (account_type_s*)account_type;
3391         label_s *label_data = (label_s*)malloc(sizeof(label_s));
3392
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;
3396         }
3397         ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
3398
3399         label_data->label = _account_get_text(label);
3400         label_data->locale = _account_get_text(locale);
3401
3402         data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
3403
3404         return ACCOUNT_ERROR_NONE;
3405 }
3406
3407
3408 int account_type_get_app_id(account_type_h account_type, char **app_id)
3409 {
3410         if (!account_type) {
3411                 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
3412                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3413         }
3414
3415         if (!app_id) {
3416                 ACCOUNT_ERROR("(%s)-(%d) app id is NULL.\n", __FUNCTION__, __LINE__);
3417                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3418         }
3419
3420         account_type_s *data = (account_type_s*)account_type;
3421
3422         (*app_id) = NULL;
3423         *app_id = _account_get_text(data->app_id);
3424
3425         return ACCOUNT_ERROR_NONE;
3426 }
3427
3428 int account_type_get_service_provider_id(account_type_h account_type, char **service_provider_id)
3429 {
3430         if (!account_type) {
3431                 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
3432                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3433         }
3434
3435         if (!service_provider_id) {
3436                 ACCOUNT_ERROR("(%s)-(%d) service provider id is NULL.\n", __FUNCTION__, __LINE__);
3437                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3438         }
3439
3440         account_type_s *data = (account_type_s*)account_type;
3441
3442         (*service_provider_id) = NULL;
3443         *service_provider_id = _account_get_text(data->service_provider_id);
3444
3445         return ACCOUNT_ERROR_NONE;
3446 }
3447
3448 int account_type_get_icon_path(account_type_h account_type, char **icon_path)
3449 {
3450         if (!account_type) {
3451                 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
3452                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3453         }
3454
3455         if (!icon_path) {
3456                 ACCOUNT_ERROR("(%s)-(%d) icon path is NULL.\n", __FUNCTION__, __LINE__);
3457                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3458         }
3459
3460         account_type_s *data = (account_type_s*)account_type;
3461
3462         (*icon_path) = NULL;
3463         *icon_path = _account_get_text(data->icon_path);
3464
3465         return ACCOUNT_ERROR_NONE;
3466 }
3467
3468 int account_type_get_small_icon_path(account_type_h account_type, char **small_icon_path)
3469 {
3470         if (!account_type) {
3471                 ACCOUNT_ERROR("(%s)-(%d) account type handle is NULL.\n", __FUNCTION__, __LINE__);
3472                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3473         }
3474
3475         if (!small_icon_path) {
3476                 ACCOUNT_ERROR("(%s)-(%d) icon path is NULL.\n", __FUNCTION__, __LINE__);
3477                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3478         }
3479
3480         account_type_s *data = (account_type_s*)account_type;
3481
3482         (*small_icon_path) = NULL;
3483         *small_icon_path = _account_get_text(data->small_icon_path);
3484
3485         return ACCOUNT_ERROR_NONE;
3486 }
3487
3488 int account_type_get_multiple_account_support(account_type_h account_type, int *multiple_account_support)
3489 {
3490         if (!account_type) {
3491                 ACCOUNT_ERROR("(%s)-(%d) account_type handle is NULL.\n", __FUNCTION__, __LINE__);
3492                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3493         }
3494         if (!multiple_account_support) {
3495                 ACCOUNT_ERROR("(%s)-(%d) multiple_account_support is NULL.\n", __FUNCTION__, __LINE__);
3496                 return ACCOUNT_ERROR_INVALID_PARAMETER;
3497         }
3498
3499         account_type_s *data = (account_type_s*)account_type;
3500
3501         *multiple_account_support = data->multiple_account_support;
3502
3503         return ACCOUNT_ERROR_NONE;
3504 }
3505
3506 int account_type_get_label(account_type_h account_type, account_label_cb cb_func, void *user_data)
3507 {
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"));
3510
3511         GSList *iter;
3512         account_type_s *data = (account_type_s*)account_type;
3513
3514         for (iter = data->label_list; iter != NULL; iter = g_slist_next(iter)) {
3515                 label_s *label_data = NULL;
3516
3517                 label_data = (label_s*)iter->data;
3518
3519                 ACCOUNT_VERBOSE("account_type_get_label :: app_id=%s, label=%s, locale=%s", label_data->app_id, label_data->label, label_data->locale);
3520
3521                 cb_func(label_data->app_id, label_data->label, label_data->locale, user_data);
3522         }
3523
3524         return ACCOUNT_ERROR_NONE;
3525 }
3526
3527 static gboolean _account_type_check_duplicated(account_type_s *data)
3528 {
3529         char query[ACCOUNT_SQL_LEN_MAX] = {0, };
3530         int count = 0;
3531
3532         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3533
3534         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId='%s'"
3535                         , ACCOUNT_TYPE_TABLE, data->app_id);
3536
3537         count = _account_get_record_count(query);
3538         if (count > 0) {
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);
3541                 return TRUE;
3542         }
3543
3544         return FALSE;
3545 }
3546
3547 static int _account_type_convert_account_to_sql(account_type_s *account_type, account_stmt hstmt, char *sql_value)
3548 {
3549         int count = 1;
3550
3551         /*Caution : Keep insert query orders.*/
3552
3553         /* 1. app id*/
3554         _account_query_bind_text(hstmt, count++, (char*)account_type->app_id);
3555
3556         /* 2. service provider id*/
3557         _account_query_bind_text(hstmt, count++, (char*)account_type->service_provider_id);
3558
3559         /* 3. icon path*/
3560         _account_query_bind_text(hstmt, count++, (char*)account_type->icon_path);
3561
3562         /* 4. small icon path*/
3563         _account_query_bind_text(hstmt, count++, (char*)account_type->small_icon_path);
3564
3565         /* 5. multiple accont support*/
3566         _account_query_bind_int(hstmt, count++, account_type->multiple_account_support);
3567
3568         return count;
3569 }
3570
3571
3572 static int _account_type_execute_insert_query(account_type_s *account_type)
3573 {
3574         int                             rc = 0;
3575         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3576         int                             error_code = ACCOUNT_ERROR_NONE;
3577         account_stmt    hstmt = NULL;
3578
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;
3584         }
3585
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;
3589         }
3590
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);
3594
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()));
3597
3598         _account_type_convert_account_to_sql(account_type, hstmt, query);
3599
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;
3604         }
3605
3606         _account_query_finalize(hstmt);
3607         hstmt = NULL;
3608
3609         return error_code;
3610 }
3611
3612 static int _account_type_insert_label(account_type_s *account_type)
3613 {
3614         int                     rc, count = 1;
3615         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3616         account_stmt    hstmt = NULL;
3617
3618         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
3619
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;
3623         }
3624
3625         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where AppId = '%s'", ACCOUNT_TYPE_TABLE, account_type->app_id);
3626
3627         rc = _account_get_record_count(query);
3628
3629         if (rc <= 0) {
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;
3632         }
3633
3634         /* insert query*/
3635         GSList *iter;
3636
3637         for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
3638                 int rc, ret;
3639                 count = 1;
3640                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3641                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
3642                                 "(?, ?, ?) ", LABEL_TABLE);
3643
3644                 hstmt = _account_prepare_query(query);
3645
3646                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
3647
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);
3651
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"));
3658
3659                 rc = _account_query_step(hstmt);
3660
3661                 if (rc != SQLITE_DONE) {
3662                         ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
3663                         break;
3664                 }
3665
3666                 _account_query_finalize(hstmt);
3667                 hstmt = NULL;
3668
3669         }
3670
3671         ACCOUNT_VERBOSE( "_account_type_insert_label() DONE\n");
3672
3673         return ACCOUNT_ERROR_NONE;
3674 }
3675
3676 int account_type_insert_to_db(account_type_h account_type, int* account_type_id)
3677 {
3678         int             error_code = ACCOUNT_ERROR_NONE;
3679
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"));
3683
3684         account_type_s *data = (account_type_s*)account_type;
3685
3686         pthread_mutex_lock(&account_mutex);
3687
3688         if (_account_type_check_duplicated(data)) {
3689                 error_code = ACCOUNT_ERROR_DUPLICATED;
3690         } else {
3691                 *account_type_id = _account_get_next_sequence(ACCOUNT_TYPE_TABLE);
3692
3693                 error_code = _account_type_execute_insert_query(data);
3694
3695                 if (error_code != ACCOUNT_ERROR_NONE)
3696                         *account_type_id = -1;
3697         }
3698
3699         ACCOUNT_INFO( "_account_type_execute_insert_query, insert error_code : %d", error_code);
3700
3701         _account_type_insert_label(data);
3702
3703         pthread_mutex_unlock(&account_mutex);
3704
3705         return ACCOUNT_ERROR_NONE;
3706 }
3707
3708 static int _account_type_update_label(account_type_s *account_type, const char* app_id)
3709 {
3710         int                     rc, count = 1;
3711         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
3712         account_stmt    hstmt = NULL;
3713
3714         ACCOUNT_RETURN_VAL((account_type != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
3715
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;
3719         }
3720
3721         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3722
3723         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId=? ", LABEL_TABLE);
3724         hstmt = _account_prepare_query(query);
3725         count = 1;
3726         _account_query_bind_text(hstmt, count++, app_id);
3727         rc = _account_query_step(hstmt);
3728
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;
3732         }
3733         _account_query_finalize(hstmt);
3734         hstmt = NULL;
3735
3736         GSList *iter;
3737
3738         for (iter = account_type->label_list; iter != NULL; iter = g_slist_next(iter)) {
3739                 int rc, ret;
3740                 count = 1;
3741                 ACCOUNT_MEMSET(query, 0x00, sizeof(query));
3742                 ACCOUNT_SNPRINTF(query, sizeof(query), "INSERT INTO %s(AppId, Label, Locale) VALUES "
3743                                 "(?, ?, ?) ", LABEL_TABLE);
3744
3745                 hstmt = _account_prepare_query(query);
3746
3747                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
3748
3749                 label_s* label_data = NULL;
3750                 label_data = (label_s*)iter->data;
3751
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"));
3758
3759                 rc = _account_query_step(hstmt);
3760
3761                 if (rc != SQLITE_DONE) {
3762                         ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
3763                         break;
3764                 }
3765                 _account_query_finalize(hstmt);
3766                 hstmt = NULL;
3767         }
3768
3769         ACCOUNT_VERBOSE( "_account_type_update_label() DONE\n");
3770
3771         return ACCOUNT_ERROR_NONE;
3772 }
3773
3774
3775 static int _account_type_update_account(account_type_s *account_type, const char* app_id)
3776 {
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;
3781
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;
3785         }
3786
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);
3790
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()));
3793
3794         binding_count = _account_type_convert_account_to_sql(account_type, hstmt, query);
3795         _account_query_bind_text(hstmt, binding_count++, app_id);
3796
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());
3800         }
3801
3802         _account_query_finalize(hstmt);
3803         hstmt = NULL;
3804
3805         /*update label*/
3806         error_code = _account_type_update_label(account_type, app_id);
3807
3808         return error_code;
3809 }
3810
3811 int account_type_update_to_db_by_app_id(account_type_h account_type, const char* app_id)
3812 {
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."));
3816
3817         int     error_code = ACCOUNT_ERROR_NONE;
3818         account_type_s* data = (account_type_s*)account_type;
3819
3820         pthread_mutex_lock(&account_mutex);
3821
3822         error_code = _account_type_update_account(data, app_id);
3823
3824         pthread_mutex_unlock(&account_mutex);
3825
3826         return ACCOUNT_ERROR_NONE;
3827 }
3828
3829 int account_type_delete_by_app_id(const char* app_id)
3830 {
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;
3838
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."));
3841
3842         /* Check requested ID to delete */
3843         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) FROM %s WHERE AppId = '%s'", ACCOUNT_TYPE_TABLE, app_id);
3844
3845         count = _account_get_record_count(query);
3846         if (count <= 0) {
3847                 ACCOUNT_ERROR("app id(%s) is not exist. count(%d)\n", app_id, count);
3848                 return ACCOUNT_ERROR_RECORD_NOT_FOUND;
3849         }
3850
3851         /* transaction control required*/
3852         ret_transaction = _account_begin_transaction();
3853
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;
3858         }
3859
3860         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ?", LABEL_TABLE);
3861
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()));
3865
3866         _account_query_bind_text(hstmt, binding_count++, app_id);
3867
3868         rc = _account_query_step(hstmt);
3869         ACCOUNT_CATCH_ERROR(rc == SQLITE_DONE, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3870
3871         _account_query_finalize(hstmt);
3872         hstmt = NULL;
3873
3874         binding_count = 1;
3875         ACCOUNT_MEMSET(query, 0, sizeof(query));
3876
3877         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AppId = ? ", ACCOUNT_TYPE_TABLE);
3878
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()));
3882
3883         _account_query_bind_text(hstmt, binding_count++, app_id);
3884
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));
3887
3888         _account_query_finalize(hstmt);
3889         is_success = TRUE;
3890
3891         hstmt = NULL;
3892
3893         CATCH:
3894         if (hstmt != NULL) {
3895                 _account_query_finalize(hstmt);
3896                 hstmt = NULL;
3897         }
3898
3899         ret_transaction = _account_end_transaction(is_success);
3900
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);
3903         }
3904
3905         pthread_mutex_unlock(&account_mutex);
3906
3907         return error_code;
3908 }
3909
3910 static void _account_type_convert_column_to_account_type(account_stmt hstmt, account_type_s *account_type_record)
3911 {
3912         char    *textbuf = NULL;
3913
3914         account_type_record->id = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_ID);
3915
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));
3918
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));
3921
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));
3924
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));
3927
3928         account_type_record->multiple_account_support = _account_query_table_column_int(hstmt, ACCOUNT_TYPE_FIELD_MULTIPLE_ACCOUNT_SUPPORT);
3929
3930         ACCOUNT_VERBOSE("END _account_type_convert_column_to_account_type");
3931 }
3932
3933 static void _account_type_convert_column_to_label(account_stmt hstmt, label_s *label_record)
3934 {
3935         char *textbuf = NULL;
3936
3937         textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_APP_ID);
3938         _account_db_data_to_text(textbuf, &(label_record->app_id));
3939
3940         textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LABEL);
3941         _account_db_data_to_text(textbuf, &(label_record->label));
3942
3943         textbuf = _account_query_table_column_text(hstmt, LABEL_FIELD_LOCALE);
3944         _account_db_data_to_text(textbuf, &(label_record->locale));
3945
3946         ACCOUNT_VERBOSE("END _account_type_convert_column_to_label");
3947 }
3948
3949 int account_type_query_label_by_app_id(account_label_cb cb_func, const char* app_id, void *user_data )
3950 {
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;
3955
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."));
3959
3960         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
3961
3962         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", LABEL_TABLE);
3963         hstmt = _account_prepare_query(query);
3964
3965         _account_query_bind_text(hstmt, binding_count++, app_id);
3966
3967         rc = _account_query_step(hstmt);
3968         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
3969
3970         label_s* label_record = NULL;
3971
3972         while (rc == SQLITE_ROW) {
3973                 label_record = (label_s*) malloc(sizeof(label_s));
3974
3975                 if (label_record == NULL) {
3976                         ACCOUNT_FATAL("malloc Failed");
3977                         break;
3978                 }
3979
3980                 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
3981
3982                 _account_type_convert_column_to_label(hstmt, label_record);
3983
3984                 cb_func(label_record->app_id, label_record->label , label_record->locale, user_data);
3985
3986                 _account_type_free_label_items(label_record);
3987                 _ACCOUNT_FREE(label_record);
3988
3989                 rc = _account_query_step(hstmt);
3990         }
3991
3992         _account_query_finalize(hstmt);
3993         hstmt = NULL;
3994
3995         error_code = ACCOUNT_ERROR_NONE;
3996
3997 CATCH:
3998         if (hstmt != NULL) {
3999                 _account_query_finalize(hstmt);
4000                 hstmt = NULL;
4001         }
4002
4003         pthread_mutex_unlock(&account_mutex);
4004         return error_code;
4005 }
4006
4007 int _account_type_label_get_app_id(label_h label, char **app_id)
4008 {
4009         if (!label) {
4010                 ACCOUNT_ERROR("(%s)-(%d) label handle is NULL.\n", __FUNCTION__, __LINE__);
4011                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4012         }
4013
4014         if (!app_id) {
4015                 ACCOUNT_ERROR("(%s)-(%d) app_id is NULL.\n", __FUNCTION__, __LINE__);
4016                 return ACCOUNT_ERROR_INVALID_PARAMETER;
4017         }
4018
4019         label_s *data = (label_s*)label;
4020
4021         (*app_id) = NULL;
4022
4023         *app_id = _account_get_text(data->app_id);
4024
4025         return ACCOUNT_ERROR_NONE;
4026 }
4027
4028 bool _account_get_label_text_cb(char* app_id, char* label, char* locale, void *user_data)
4029 {
4030         account_type_s *data = (account_type_s*)user_data;
4031
4032         label_s *label_data = (label_s*)malloc(sizeof(label_s));
4033
4034         if (label_data == NULL) {
4035                 ACCOUNT_FATAL("_account_get_label_text_cb : MALLOC FAIL\n");
4036                 return FALSE;
4037         }
4038         ACCOUNT_MEMSET(label_data, 0, sizeof(label_s));
4039
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);
4043
4044         data->label_list = g_slist_append(data->label_list, (gpointer)label_data);
4045
4046         ACCOUNT_VERBOSE("_account_get_label_text_cb :: appid=%s, label=%s\n", label_data->app_id, label_data->label);
4047
4048         return TRUE;
4049 }
4050
4051
4052 int account_type_query_by_app_id(const char* app_id, account_type_h *account_type)
4053 {
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;
4058
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."));
4062
4063         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4064
4065         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ?", ACCOUNT_TYPE_TABLE);
4066         hstmt = _account_prepare_query(query);
4067
4068         _account_query_bind_text(hstmt, binding_count++, app_id);
4069
4070         rc = _account_query_step(hstmt);
4071         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4072
4073         account_type_s *account_type_record = (account_type_s *)(*account_type);
4074
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);
4079         }
4080
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);
4083
4084         hstmt = NULL;
4085         error_code = ACCOUNT_ERROR_NONE;
4086
4087         CATCH:
4088         if (hstmt != NULL) {
4089                 _account_query_finalize(hstmt);
4090                 hstmt = NULL;
4091         }
4092
4093         pthread_mutex_unlock(&account_mutex);
4094         return error_code;
4095 }
4096
4097 int account_type_foreach_account_type_from_db(account_type_cb callback, void *user_data)
4098 {
4099         int                     error_code = ACCOUNT_ERROR_NONE;
4100         account_stmt    hstmt = NULL;
4101         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4102         int                     rc = 0;
4103         GList                   *account_type_list = NULL;
4104
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."));
4107
4108         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4109
4110         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s ", ACCOUNT_TYPE_TABLE);
4111         hstmt = _account_prepare_query(query);
4112
4113         rc = _account_query_step(hstmt);
4114
4115         account_type_s *account_type_record = NULL;
4116
4117         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4118
4119         int k=0;
4120         while(rc == SQLITE_ROW) {
4121                 account_type_record = (account_type_s*) malloc(sizeof(account_type_s));
4122
4123                 if (account_type_record == NULL) {
4124                         ACCOUNT_FATAL("malloc Failed");
4125                         break;
4126                 }
4127
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);
4132                 k++;
4133         }
4134
4135         _account_query_finalize(hstmt);
4136         hstmt = NULL;
4137
4138         GList* iter;
4139         k = 0;
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);
4145                 k++;
4146         }
4147
4148         error_code = ACCOUNT_ERROR_NONE;
4149
4150 CATCH:
4151         if (hstmt != NULL) {
4152                 _account_query_finalize(hstmt);
4153                 hstmt = NULL;
4154         }
4155         if (account_type_list) {
4156                 _account_type_glist_free(account_type_list);
4157                 account_type_list = NULL;
4158         }
4159
4160         return error_code;
4161 }
4162
4163 // output parameter label must be free
4164 int account_type_query_label_by_locale(const char* app_id, const char* locale, char** label)
4165 {
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;
4170
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"));
4174
4175         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4176
4177         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AppId = ? AND Locale = '%s' ", LABEL_TABLE, locale);
4178         hstmt = _account_prepare_query(query);
4179
4180         _account_query_bind_text(hstmt, binding_count++, app_id);
4181
4182         rc = _account_query_step(hstmt);
4183         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4184
4185         label_s* label_record = NULL;
4186
4187         while (rc == SQLITE_ROW) {
4188                 label_record = (label_s*) malloc(sizeof(label_s));
4189
4190                 if (label_record == NULL) {
4191                         ACCOUNT_FATAL("malloc Failed");
4192                         break;
4193                 }
4194
4195                 ACCOUNT_MEMSET(label_record, 0x00, sizeof(label_s));
4196
4197                 _account_type_convert_column_to_label(hstmt,label_record);
4198
4199                 *label = _account_get_text(label_record->label);
4200
4201                 _account_type_free_label_items(label_record);
4202                 _ACCOUNT_FREE(label_record);
4203
4204                 rc = _account_query_step(hstmt);
4205         }
4206
4207         _account_query_finalize(hstmt);
4208         hstmt = NULL;
4209
4210         error_code = ACCOUNT_ERROR_NONE;
4211
4212         CATCH:
4213         if (hstmt != NULL) {
4214                 _account_query_finalize(hstmt);
4215                 hstmt = NULL;
4216         }
4217
4218         pthread_mutex_unlock(&account_mutex);
4219         return error_code;
4220 }
4221
4222 static int _account_insert_custom(account_s *account, int account_id)
4223 {
4224         int                     rc, count = 1;
4225         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4226         account_stmt    hstmt = NULL;
4227
4228         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4229
4230         if (g_slist_length( account->custom_list)==0) {
4231                 ACCOUNT_ERROR( "_account_insert_custom, no custom data\n");
4232                 return ACCOUNT_ERROR_NONE;
4233         }
4234
4235         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
4236
4237         rc = _account_get_record_count(query);
4238
4239         if (rc <= 0) {
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;
4242         }
4243
4244         /* insert query*/
4245
4246         GSList *iter;
4247
4248         for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
4249                 int rc, ret;
4250                 count = 1;
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);
4254
4255                 hstmt = _account_prepare_query(query);
4256
4257                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4258
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);
4262
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"));
4271
4272                 rc = _account_query_step(hstmt);
4273
4274                 if (rc != SQLITE_DONE) {
4275                         ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4276                         break;
4277                 }
4278
4279                 _account_query_finalize(hstmt);
4280                 hstmt = NULL;
4281
4282         }
4283
4284         ACCOUNT_VERBOSE( "_account_insert_custom() DONE\n");
4285
4286         return ACCOUNT_ERROR_NONE;
4287 }
4288
4289 static int _account_update_custom(account_s *account, int account_id)
4290 {
4291         int                     rc, count = 1;
4292         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4293         account_stmt    hstmt = NULL;
4294
4295         ACCOUNT_RETURN_VAL((account != NULL), {}, ACCOUNT_ERROR_INVALID_PARAMETER, ("ACCOUNT HANDLE IS NULL"));
4296
4297         if (g_slist_length( account->custom_list)==0) {
4298                 ACCOUNT_ERROR( "_account_update_custom, no custom data\n");
4299                 return ACCOUNT_ERROR_NONE;
4300         }
4301
4302         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT COUNT(*) from %s where _id=%d", ACCOUNT_TABLE, account_id);
4303
4304         rc = _account_get_record_count(query);
4305
4306         if (rc <= 0) {
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;
4309         }
4310
4311         ACCOUNT_MEMSET(query, 0x00, sizeof(query));
4312
4313         ACCOUNT_SNPRINTF(query, sizeof(query), "DELETE FROM %s WHERE AccountId=? ", ACCOUNT_CUSTOM_TABLE);
4314         hstmt = _account_prepare_query(query);
4315         count = 1;
4316         _account_query_bind_int(hstmt, count++, (int)account_id);
4317         rc = _account_query_step(hstmt);
4318
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;
4322         }
4323         _account_query_finalize(hstmt);
4324         hstmt = NULL;
4325
4326         GSList *iter;
4327
4328         for (iter = account->custom_list; iter != NULL; iter = g_slist_next(iter)) {
4329                 int rc, ret;
4330                 count = 1;
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);
4334
4335                 hstmt = _account_prepare_query(query);
4336
4337                 ACCOUNT_RETURN_VAL((hstmt != NULL), {}, ACCOUNT_ERROR_DB_FAILED, ("_account_prepare_query() failed(%s).\n", _account_db_err_msg()));
4338
4339                 account_custom_s* custom_data = NULL;
4340                 custom_data = (account_custom_s*)iter->data;
4341
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"));
4350
4351                 rc = _account_query_step(hstmt);
4352
4353                 if (rc != SQLITE_DONE) {
4354                         ACCOUNT_FATAL( "_account_query_step() failed(%d, %s)", rc, _account_db_err_msg());
4355                         break;
4356                 }
4357
4358                 _account_query_finalize(hstmt);
4359                 hstmt = NULL;
4360
4361         }
4362
4363         ACCOUNT_VERBOSE( "_account_update_custom() DONE\n");
4364
4365         return ACCOUNT_ERROR_NONE;
4366 }
4367
4368 static int _account_query_custom_by_account_id(account_custom_cb cb_func, int account_id, void *user_data )
4369 {
4370         int                     error_code = ACCOUNT_ERROR_NONE;
4371         account_stmt    hstmt = NULL;
4372         char                    query[ACCOUNT_SQL_LEN_MAX] = {0, };
4373         int                     rc = 0;
4374
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."));
4378
4379         ACCOUNT_MEMSET(query, 0x00, ACCOUNT_SQL_LEN_MAX);
4380
4381         ACCOUNT_SNPRINTF(query, sizeof(query), "SELECT * FROM %s WHERE AccountId = %d", ACCOUNT_CUSTOM_TABLE, account_id);
4382         hstmt = _account_prepare_query(query);
4383
4384         rc = _account_query_step(hstmt);
4385         ACCOUNT_CATCH_ERROR(rc == SQLITE_ROW, {}, ACCOUNT_ERROR_RECORD_NOT_FOUND, ("The record isn't found.\n"));
4386
4387         account_custom_s* custom_record = NULL;
4388
4389         while (rc == SQLITE_ROW) {
4390                 custom_record = (account_custom_s*) malloc(sizeof(account_custom_s));
4391
4392                 if (custom_record == NULL) {
4393                         ACCOUNT_FATAL("malloc Failed");
4394                         break;
4395                 }
4396
4397                 ACCOUNT_MEMSET(custom_record, 0x00, sizeof(account_custom_s));
4398
4399                 _account_convert_column_to_custom(hstmt, custom_record);
4400
4401                 cb_func(custom_record->key, custom_record->value, user_data);
4402
4403                 _account_custom_item_free(custom_record);
4404                 _ACCOUNT_FREE(custom_record);
4405
4406                 rc = _account_query_step(hstmt);
4407         }
4408
4409         _account_query_finalize(hstmt);
4410         hstmt = NULL;
4411
4412         error_code = ACCOUNT_ERROR_NONE;
4413
4414 CATCH:
4415         if (hstmt != NULL) {
4416                 _account_query_finalize(hstmt);
4417                 hstmt = NULL;
4418         }
4419
4420         pthread_mutex_unlock(&account_mutex);
4421         return error_code;
4422 }
4423
4424