4 * Copyright (c) 2012 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 #include "calendar_db.h"
22 #include "cal_internal.h"
23 #include "cal_typedef.h"
25 #include "cal_record.h"
28 #include "cal_db_query.h"
29 #include "cal_db_plugin_book_helper.h"
30 #include "cal_db_util.h"
32 int cal_db_delete_account(int account_id)
35 int ret = CALENDAR_ERROR_NONE;
36 char query[CAL_DB_SQL_MAX_LEN] = {0};
37 sqlite3_stmt *stmt = NULL;
38 GList *calendar_list = NULL;
40 snprintf(query, sizeof(query), "SELECT id FROM %s where account_id = %d and deleted = 0",
41 CAL_TABLE_CALENDAR, account_id);
43 ret = cal_db_util_query_prepare(query, &stmt);
44 if (CALENDAR_ERROR_NONE != ret) {
46 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
47 SECURE("query[%s]", query);
52 while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
54 id = sqlite3_column_int(stmt, 0);
55 calendar_list = g_list_append(calendar_list, GINT_TO_POINTER(id));
58 sqlite3_finalize(stmt);
60 DBG("calendar cnt=%d", g_list_length(calendar_list));
62 ret = cal_db_util_begin_trans();
63 if (CALENDAR_ERROR_NONE != ret) {
65 ERR("cal_db_util_begin_trans() Fail(%d)", ret);
66 g_list_free(calendar_list);
67 return CALENDAR_ERROR_DB_FAILED;
71 GList* cursor = calendar_list;
73 int id = GPOINTER_TO_INT(cursor->data);
75 ret = cal_db_delete_record(_calendar_book._uri, id);
76 if (CALENDAR_ERROR_NONE != ret) {
78 ERR("cal_db_delete_record() Fail(%d)", ret);
79 SECURE("book_id(%d)", id);
82 cursor = g_list_next(cursor);
85 g_list_free(calendar_list);
86 cal_db_util_end_trans(true);
87 return CALENDAR_ERROR_NONE;