add comment LCOV_EXCL
[platform/core/pim/calendar-service.git] / server / db / cal_db_plugin_attendee_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_util.h"
29 #include "cal_db_query.h"
30 #include "cal_db_plugin_attendee_helper.h"
31 #include "cal_db.h"
32 #include "cal_utils.h"
33
34 static int _cal_db_attendee_insert_record(calendar_record_h record, int parent_id)
35 {
36         int index;
37         int ret = 0;
38         char query[CAL_DB_SQL_MAX_LEN];
39         sqlite3_stmt *stmt;
40         cal_attendee_s *attendee = NULL;
41
42         attendee = (cal_attendee_s *)(record);
43         RETVM_IF(NULL == attendee, CALENDAR_ERROR_INVALID_PARAMETER,
44                         "Invalid argument: cal_alarm_s is NULL");
45
46         snprintf(query, sizeof(query),
47                         "INSERT INTO %s ("
48                         "event_id, "
49                         "attendee_name, attendee_email, attendee_number, "
50                         "attendee_status, attendee_cutype, attendee_ct_index, "
51                         "attendee_role, attendee_rsvp, attendee_group, "
52                         "attendee_delegator_uri, attendee_delegatee_uri, "
53                         "attendee_member, attendee_uid "
54                         ") VALUES ("
55                         "%d, "
56                         "?, ?, ?, "
57                         "%d, %d, %d, "
58                         "%d, %d, ?, "
59                         "?, ?, "
60                         "?, ?)",
61                         CAL_TABLE_ATTENDEE,
62                         parent_id,
63                         attendee->attendee_status,
64                         attendee->attendee_cutype,
65                         attendee->attendee_ct_index,
66                         attendee->attendee_role,
67                         attendee->attendee_rsvp);
68
69         ret = cal_db_util_query_prepare(query, &stmt);
70         if (CALENDAR_ERROR_NONE != ret) {
71                 /* LCOV_EXCL_START */
72                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
73                 SECURE("query[%s]", query);
74                 return ret;
75                 /* LCOV_EXCL_STOP */
76         }
77
78         index = 1;
79         if (attendee->attendee_name)
80                 cal_db_util_stmt_bind_text(stmt, index, attendee->attendee_name);
81         index++;
82
83         if (attendee->attendee_email)
84                 cal_db_util_stmt_bind_text(stmt, index, attendee->attendee_email);
85         index++;
86
87         if (attendee->attendee_number)
88                 cal_db_util_stmt_bind_text(stmt, index, attendee->attendee_number);
89         index++;
90
91         if (attendee->attendee_group)
92                 cal_db_util_stmt_bind_text(stmt, index, attendee->attendee_group);
93         index++;
94
95         if (attendee->attendee_delegator_uri)
96                 cal_db_util_stmt_bind_text(stmt, index, attendee->attendee_delegator_uri);
97         index++;
98
99         if (attendee->attendee_delegatee_uri)
100                 cal_db_util_stmt_bind_text(stmt, index, attendee->attendee_delegatee_uri);
101         index++;
102
103         if (attendee->attendee_member)
104                 cal_db_util_stmt_bind_text(stmt, index, attendee->attendee_member);
105         index++;
106
107         if (attendee->attendee_uid)
108                 cal_db_util_stmt_bind_text(stmt, index, attendee->attendee_uid);
109         index++;
110
111         ret = cal_db_util_stmt_step(stmt);
112         sqlite3_finalize(stmt);
113         if (CALENDAR_ERROR_NONE != ret) {
114                 /* LCOV_EXCL_START */
115                 ERR("cal_db_util_stmt_step() Fail(%d)", ret);
116                 return ret;
117                 /* LCOV_EXCL_STOP */
118         }
119
120         attendee->parent_id = parent_id;
121         return CALENDAR_ERROR_NONE;
122 }
123
124 int cal_db_attendee_insert_records(cal_list_s *list_s, int parent_id)
125 {
126         int ret;
127         int count = 0;
128         calendar_record_h record = NULL;
129         calendar_list_h list = (calendar_list_h)list_s;
130
131         RETV_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER);
132
133         calendar_list_get_count(list, &count);
134         if (0 == count)
135                 return CALENDAR_ERROR_NONE;
136
137         calendar_list_first(list);
138         while (CALENDAR_ERROR_NONE == calendar_list_get_current_record_p(list, &record)) {
139                 ret = _cal_db_attendee_insert_record(record, parent_id);
140                 RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "cal_db_extended_insert_record() Fail(%d)", ret);
141                 calendar_list_next(list);
142         }
143         return CALENDAR_ERROR_NONE;
144
145 }
146
147 int cal_db_attendee_get_records(int parent_id, cal_list_s *list)
148 {
149         int ret;
150         char query[CAL_DB_SQL_MAX_LEN] = {0};
151         sqlite3_stmt *stmt = NULL;
152
153         RETVM_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER,
154                         "Invalid parameter: list is NULL");
155
156         snprintf(query, sizeof(query),
157                         "SELECT rowid, "
158                         "attendee_name, "
159                         "attendee_email, "
160                         "attendee_number, "
161                         "attendee_status, "
162                         "attendee_ct_index, "
163                         "attendee_role, "
164                         "attendee_rsvp, "
165                         "attendee_group, "
166                         "attendee_delegator_uri, "
167                         "attendee_uid, "
168                         "attendee_cutype, "
169                         "attendee_delegatee_uri, "
170                         "attendee_member "
171                         "FROM %s WHERE event_id = %d ",
172                         CAL_TABLE_ATTENDEE, parent_id);
173
174         ret = cal_db_util_query_prepare(query, &stmt);
175         if (CALENDAR_ERROR_NONE != ret) {
176                 /* LCOV_EXCL_START */
177                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
178                 SECURE("query[%s]", query);
179                 return ret;
180                 /* LCOV_EXCL_STOP */
181         }
182
183         int index;
184         const unsigned char *temp;
185         calendar_record_h record = NULL;
186         cal_attendee_s *attendee = NULL;
187
188         while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
189                 ret = calendar_record_create(_calendar_attendee._uri, &record);
190                 if (CALENDAR_ERROR_NONE != ret) {
191                         /* LCOV_EXCL_START */
192                         ERR("calendar_record_create() Fail(%d)", ret);
193                         sqlite3_finalize(stmt);
194                         cal_list_clear(list);
195                         return ret;
196                         /* LCOV_EXCL_STOP */
197                 }
198
199                 index = 0;
200                 attendee = (cal_attendee_s *)(record);
201
202                 attendee->parent_id = parent_id;
203                 attendee->id = sqlite3_column_int(stmt, index++);
204
205                 temp = sqlite3_column_text(stmt, index++);
206                 attendee->attendee_name = cal_strdup((const char*)temp);
207
208                 temp = sqlite3_column_text(stmt, index++);
209                 attendee->attendee_email = cal_strdup((const char*)temp);
210
211                 temp = sqlite3_column_text(stmt, index++);
212                 attendee->attendee_number = cal_strdup((const char*)temp);
213
214                 attendee->attendee_status = sqlite3_column_int(stmt, index++);
215                 attendee->attendee_ct_index = sqlite3_column_int(stmt, index++);
216                 attendee->attendee_role = sqlite3_column_int(stmt, index++);
217                 attendee->attendee_rsvp = sqlite3_column_int(stmt, index++);
218
219                 temp = sqlite3_column_text(stmt, index++);
220                 attendee->attendee_group = cal_strdup((const char*)temp);
221
222                 temp = sqlite3_column_text(stmt, index++);
223                 attendee->attendee_delegator_uri = cal_strdup((const char*)temp);
224
225                 temp = sqlite3_column_text(stmt, index++);
226                 attendee->attendee_uid = cal_strdup((const char*)temp);
227
228                 attendee->attendee_cutype = sqlite3_column_int(stmt, index++);
229
230                 temp = sqlite3_column_text(stmt, index++);
231                 attendee->attendee_delegatee_uri = cal_strdup((const char*)temp);
232
233                 temp = sqlite3_column_text(stmt, index++);
234                 attendee->attendee_member = cal_strdup((const char*)temp);
235
236                 calendar_list_add((calendar_list_h)list, record);
237         }
238
239         sqlite3_finalize(stmt);
240         return CALENDAR_ERROR_NONE;
241 }
242
243 int cal_db_attendee_delete_with_id(int parent_id)
244 {
245         int ret = 0;
246         char query[CAL_DB_SQL_MAX_LEN] = {0};
247
248         snprintf(query, sizeof(query), "DELETE FROM %s WHERE event_id=%d ",
249                         CAL_TABLE_ATTENDEE, parent_id);
250
251         ret = cal_db_util_query_exec(query);
252         if (CALENDAR_ERROR_NONE != ret) {
253                 /* LCOV_EXCL_START */
254                 ERR("cal_db_util_query_exec() Fail(%d)", ret);
255                 SECURE("[%s]", query);
256                 return ret;
257                 /* LCOV_EXCL_STOP */
258         }
259
260         return CALENDAR_ERROR_NONE;
261 }
262