add comment LCOV_EXCL
[platform/core/pim/calendar-service.git] / server / db / cal_db_plugin_extended_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
20 #include <stdlib.h>
21
22 #include "cal_internal.h"
23 #include "cal_typedef.h"
24 #include "cal_view.h"
25 #include "cal_record.h"
26 #include "cal_list.h"
27
28 #include "cal_db.h"
29 #include "cal_db_util.h"
30 #include "cal_db_query.h"
31 #include "cal_db_plugin_extended_helper.h"
32 #include "cal_utils.h"
33
34 int cal_db_extended_get_records(int record_id, calendar_record_type_e record_type, cal_list_s *list)
35 {
36         int ret;
37         char query[CAL_DB_SQL_MAX_LEN] = {0};
38         sqlite3_stmt *stmt = NULL;
39
40         RETV_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER);
41
42         snprintf(query, sizeof(query),
43                         "SELECT * FROM %s "
44                         "WHERE record_id = %d AND "
45                         "record_type = %d",
46                         CAL_TABLE_EXTENDED,
47                         record_id,
48                         record_type);
49
50         ret = cal_db_util_query_prepare(query, &stmt);
51         if (CALENDAR_ERROR_NONE != ret) {
52                 /* LCOV_EXCL_START */
53                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
54                 SECURE("query[%s]", query);
55                 return ret;
56                 /* LCOV_EXCL_STOP */
57         }
58
59         int count = 0;
60         const unsigned char *temp;
61         calendar_record_h record = NULL;
62         cal_extended_s *extended = NULL;
63
64         while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
65                 ret = calendar_record_create(_calendar_extended_property._uri, &record);
66                 if (CALENDAR_ERROR_NONE != ret) {
67                         /* LCOV_EXCL_START */
68                         sqlite3_finalize(stmt);
69                         cal_list_clear(list);
70                         return ret;
71                         /* LCOV_EXCL_STOP */
72                 }
73
74                 count = 0;
75                 extended = (cal_extended_s *)(record);
76
77                 extended->id = sqlite3_column_int(stmt, count++);
78                 extended->record_id = sqlite3_column_int(stmt, count++);
79                 extended->record_type = sqlite3_column_int(stmt, count++);
80                 temp = sqlite3_column_text(stmt, count++);
81                 extended->key = cal_strdup((const char*)temp);
82                 temp = sqlite3_column_text(stmt, count++);
83                 extended->value = cal_strdup((const char*)temp);
84
85                 calendar_list_add((calendar_list_h)list, record);
86         }
87         sqlite3_finalize(stmt);
88         return CALENDAR_ERROR_NONE;
89 }
90
91 int cal_db_extended_delete_with_id(int record_id, calendar_record_type_e record_type)
92 {
93         char query[CAL_DB_SQL_MAX_LEN] = {0};
94         int ret = 0;
95
96         snprintf(query, sizeof(query), "DELETE FROM %s WHERE record_id=%d AND record_type=%d",
97                         CAL_TABLE_EXTENDED, record_id, record_type);
98
99         ret = cal_db_util_query_exec(query);
100         if (CALENDAR_ERROR_NONE != ret) {
101                 /* LCOV_EXCL_START */
102                 ERR("cal_db_util_query_exec() Fail(%d)", ret);
103                 SECURE("[%s]", query);
104                 return ret;
105                 /* LCOV_EXCL_STOP */
106         }
107
108         return CALENDAR_ERROR_NONE;
109 }
110
111 int cal_db_extended_insert_record(calendar_record_h record, int record_id, calendar_record_type_e record_type, int *id)
112 {
113         int index;
114         sqlite3_stmt *stmt;
115         char query[CAL_DB_SQL_MAX_LEN];
116         cal_extended_s* extended =  (cal_extended_s*)(record);
117         int ret;
118
119         RETV_IF(NULL == extended, CALENDAR_ERROR_INVALID_PARAMETER);
120         RETVM_IF(record_id <= 0, CALENDAR_ERROR_INVALID_PARAMETER, "record_id(%d)", record_id);
121
122         snprintf(query, sizeof(query), "INSERT INTO %s(record_id, "
123                         "record_type ,key ,value) "
124                         "VALUES(%d,%d,?,?)",
125                         CAL_TABLE_EXTENDED,
126                         record_id,
127                         record_type);
128
129         ret = cal_db_util_query_prepare(query, &stmt);
130         if (CALENDAR_ERROR_NONE != ret) {
131                 /* LCOV_EXCL_START */
132                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
133                 SECURE("query[%s]", query);
134                 return ret;
135                 /* LCOV_EXCL_STOP */
136         }
137
138         if (extended->key)
139                 cal_db_util_stmt_bind_text(stmt, 1, extended->key);
140
141         if (extended->value)
142                 cal_db_util_stmt_bind_text(stmt, 2, extended->value);
143
144         ret = cal_db_util_stmt_step(stmt);
145         sqlite3_finalize(stmt);
146         if (CALENDAR_ERROR_NONE != ret) {
147                 /* LCOV_EXCL_START */
148                 ERR("cal_db_util_stmt_step() Fail(%d)", ret);
149                 return ret;
150                 /* LCOV_EXCL_STOP */
151         }
152         index = cal_db_util_last_insert_id();
153
154         /* cal_record_set_int(record, _calendar_extended.id,index); */
155         if (id)
156                 *id = index;
157
158         if (record_type == CALENDAR_RECORD_TYPE_EVENT || record_type == CALENDAR_RECORD_TYPE_TODO) {
159                 snprintf(query, sizeof(query), "UPDATE %s SET has_extended = 1 WHERE id = %d ",
160                                 CAL_TABLE_SCHEDULE, record_id);
161                 ret = cal_db_util_query_exec(query);
162                 if (CALENDAR_ERROR_NONE != ret) {
163                         /* LCOV_EXCL_START */
164                         ERR("cal_db_util_query_exec() Fail(%d)", ret);
165                         SECURE("[%s]", query);
166                         return ret;
167                         /* LCOV_EXCL_STOP */
168                 }
169         }
170         return CALENDAR_ERROR_NONE;
171 }
172
173 int cal_db_extended_insert_records(cal_list_s *list_s, int record_id, calendar_record_type_e record_type)
174 {
175         int ret;
176         int count = 0;
177         calendar_record_h record = NULL;
178         calendar_list_h list = (calendar_list_h)list_s;
179
180         RETV_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER);
181
182         calendar_list_get_count(list, &count);
183         if (0 == count)
184                 return CALENDAR_ERROR_NONE;
185
186         calendar_list_first(list);
187         while (CALENDAR_ERROR_NONE == calendar_list_get_current_record_p(list, &record)) {
188                 ret = cal_db_extended_insert_record(record, record_id, record_type, NULL);
189                 RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "cal_db_extended_insert_record() Fail(%d)", ret);
190                 calendar_list_next(list);
191         }
192         return CALENDAR_ERROR_NONE;
193 }
194