if db version is the latest, skip checking version logic
[platform/core/pim/calendar-service.git] / server / db / cal_db_plugin_book_helper.c
1 /*
2  * Calendar Service
3  *
4  * Copyright (c) 2012 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19 #include <stdlib.h>
20
21 #include "calendar_db.h"
22 #include "cal_internal.h"
23 #include "cal_typedef.h"
24 #include "cal_view.h"
25 #include "cal_record.h"
26
27 #include "cal_db.h"
28 #include "cal_db_query.h"
29 #include "cal_db_plugin_book_helper.h"
30 #include "cal_db_util.h"
31
32 int cal_db_delete_account(int account_id)
33 {
34         CAL_FN_CALL();
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;
39
40         snprintf(query, sizeof(query), "SELECT id FROM %s where account_id = %d and deleted = 0",
41                         CAL_TABLE_CALENDAR, account_id);
42
43         ret = cal_db_util_query_prepare(query, &stmt);
44         if (CALENDAR_ERROR_NONE != ret) {
45                 /* LCOV_EXCL_START */
46                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
47                 SECURE("query[%s]", query);
48                 return ret;
49                 /* LCOV_EXCL_STOP */
50         }
51
52         while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
53                 int id = 0;
54                 id = sqlite3_column_int(stmt, 0);
55                 calendar_list = g_list_append(calendar_list, GINT_TO_POINTER(id));
56         }
57
58         sqlite3_finalize(stmt);
59         if (calendar_list)
60                 DBG("calendar cnt=%d", g_list_length(calendar_list));
61
62         ret = cal_db_util_begin_trans();
63         if (CALENDAR_ERROR_NONE != ret) {
64                 /* LCOV_EXCL_START */
65                 ERR("cal_db_util_begin_trans() Fail(%d)", ret);
66                 g_list_free(calendar_list);
67                 return CALENDAR_ERROR_DB_FAILED;
68                 /* LCOV_EXCL_STOP */
69         }
70
71         GList* cursor = calendar_list;
72         while (cursor) {
73                 int id = GPOINTER_TO_INT(cursor->data);
74
75                 ret = cal_db_delete_record(_calendar_book._uri, id);
76                 if (CALENDAR_ERROR_NONE != ret) {
77                 /* LCOV_EXCL_START */
78                         ERR("cal_db_delete_record() Fail(%d)", ret);
79                         SECURE("book_id(%d)", id);
80                 /* LCOV_EXCL_STOP */
81                 }
82                 cursor = g_list_next(cursor);
83         }
84
85         g_list_free(calendar_list);
86         cal_db_util_end_trans(true);
87         return CALENDAR_ERROR_NONE;
88 }