add comment LCOV_EXCL
[platform/core/pim/calendar-service.git] / server / db / cal_db_plugin_attendee.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
27 #include "cal_db_util.h"
28 #include "cal_db_query.h"
29 #include "cal_db_plugin_attendee_helper.h"
30 #include "cal_utils.h"
31
32 static int _cal_db_attendee_get_all_records(int offset, int limit, calendar_list_h* out_list);
33 static int _cal_db_attendee_get_records_with_query(calendar_query_h query, int offset, int limit, calendar_list_h* out_list);
34 static int _cal_db_attendee_get_count(int *out_count);
35 static int _cal_db_attendee_get_count_with_query(calendar_query_h query, int *out_count);
36 /* static int _cal_db_attendee_get_record(int id, calendar_record_h* out_record) */
37
38 cal_db_plugin_cb_s cal_db_attendee_plugin_cb = {
39         .is_query_only = false,
40         .insert_record = NULL,
41         .update_record = NULL,
42         .replace_record = NULL,
43         .delete_record = NULL,
44         .insert_records = NULL,
45         .update_records = NULL,
46         .delete_records = NULL,
47         .replace_records = NULL,
48         .get_record = NULL,
49         .get_all_records = _cal_db_attendee_get_all_records,
50         .get_records_with_query = _cal_db_attendee_get_records_with_query,
51         .get_count = _cal_db_attendee_get_count,
52         .get_count_with_query = _cal_db_attendee_get_count_with_query
53 };
54
55 static void _cal_db_attendee_get_stmt(sqlite3_stmt *stmt, calendar_record_h record)
56 {
57         cal_attendee_s *attendee = NULL;
58         int index;
59         const unsigned char *temp;
60
61         attendee = (cal_attendee_s*)(record);
62         index = 0;
63
64         attendee->parent_id = sqlite3_column_int(stmt, index++);
65
66         temp = sqlite3_column_text(stmt, index++);
67         attendee->attendee_name = cal_strdup((const char*)temp);
68
69         temp = sqlite3_column_text(stmt, index++);
70         attendee->attendee_email = cal_strdup((const char*)temp);
71
72         temp = sqlite3_column_text(stmt, index++);
73         attendee->attendee_number = cal_strdup((const char*)temp);
74
75         attendee->attendee_status = sqlite3_column_int(stmt, index++);
76         attendee->attendee_ct_index = sqlite3_column_int(stmt, index++);
77         attendee->attendee_role = sqlite3_column_int(stmt, index++);
78         attendee->attendee_rsvp = sqlite3_column_int(stmt, index++);
79
80         temp = sqlite3_column_text(stmt, index++);
81         attendee->attendee_group = cal_strdup((const char*)temp);
82
83         temp = sqlite3_column_text(stmt, index++);
84         attendee->attendee_delegator_uri = cal_strdup((const char*)temp);
85
86         temp = sqlite3_column_text(stmt, index++);
87         attendee->attendee_uid = cal_strdup((const char*)temp);
88
89         attendee->attendee_cutype = sqlite3_column_int(stmt, index++);
90
91         temp = sqlite3_column_text(stmt, index++);
92         attendee->attendee_delegatee_uri = cal_strdup((const char*)temp);
93
94         temp = sqlite3_column_text(stmt, index++);
95         attendee->attendee_member = cal_strdup((const char*)temp);
96
97         attendee->id = sqlite3_column_int(stmt, index++);
98 }
99
100 static int _cal_db_attendee_get_all_records(int offset, int limit, calendar_list_h* out_list)
101 {
102         int ret;
103         char query[CAL_DB_SQL_MAX_LEN] = {0};
104         char offsetquery[CAL_DB_SQL_MAX_LEN] = {0};
105         char limitquery[CAL_DB_SQL_MAX_LEN] = {0};
106         sqlite3_stmt *stmt = NULL;
107
108         RETV_IF(NULL == out_list, CALENDAR_ERROR_INVALID_PARAMETER);
109
110         ret = calendar_list_create(out_list);
111         RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "calendar_list_create() Fail(%d)", ret);
112
113         if (0 < limit)
114                 snprintf(limitquery, sizeof(limitquery), "LIMIT %d ", limit);
115
116         if (0 < offset)
117                 snprintf(offsetquery, sizeof(offsetquery), "OFFSET %d ", offset);
118
119         snprintf(query, sizeof(query), "SELECT *, rowid FROM %s %s %s ",
120                         CAL_TABLE_ATTENDEE, limitquery, offsetquery);
121
122         ret = cal_db_util_query_prepare(query, &stmt);
123         if (CALENDAR_ERROR_NONE != ret) {
124                 /* LCOV_EXCL_START */
125                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
126                 SECURE("query[%s]", query);
127                 calendar_list_destroy(*out_list, true);
128                 *out_list = NULL;
129                 return ret;
130                 /* LCOV_EXCL_STOP */
131         }
132
133         calendar_record_h record = NULL;
134
135         while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt))   {
136                 ret = calendar_record_create(_calendar_attendee._uri, &record);
137                 if (CALENDAR_ERROR_NONE != ret) {
138                         /* LCOV_EXCL_START */
139                         ERR("calendar_record_create() Fail(%d)", ret);
140                         calendar_list_destroy(*out_list, true);
141                         *out_list = NULL;
142                         sqlite3_finalize(stmt);
143                         return ret;
144                         /* LCOV_EXCL_STOP */
145                 }
146                 _cal_db_attendee_get_stmt(stmt, record);
147
148                 ret = calendar_list_add(*out_list, record);
149                 if (CALENDAR_ERROR_NONE != ret) {
150                         /* LCOV_EXCL_START */
151                         ERR("calendar_list_add() Fail(%d)", ret);
152                         calendar_list_destroy(*out_list, true);
153                         *out_list = NULL;
154                         calendar_record_destroy(record, true);
155                         sqlite3_finalize(stmt);
156                         return ret;
157                         /* LCOV_EXCL_STOP */
158                 }
159         }
160         sqlite3_finalize(stmt);
161
162         return CALENDAR_ERROR_NONE;
163 }
164
165 static void _cal_db_attendee_get_property_stmt(sqlite3_stmt *stmt,
166                 unsigned int property, int *stmt_count, calendar_record_h record)
167 {
168         cal_attendee_s *attendee = NULL;
169         const unsigned char *temp;
170
171         attendee = (cal_attendee_s*)(record);
172
173         switch (property) {
174         case CAL_PROPERTY_ATTENDEE_NUMBER:
175                 temp = sqlite3_column_text(stmt, *stmt_count);
176                 attendee->attendee_number = cal_strdup((const char*)temp);
177                 break;
178         case CAL_PROPERTY_ATTENDEE_CUTYPE:
179                 attendee->attendee_cutype = sqlite3_column_int(stmt, *stmt_count);
180                 break;
181         case CAL_PROPERTY_ATTENDEE_CT_INDEX:
182                 attendee->attendee_ct_index = sqlite3_column_int(stmt, *stmt_count);
183                 break;
184         case CAL_PROPERTY_ATTENDEE_UID:
185                 temp = sqlite3_column_text(stmt, *stmt_count);
186                 attendee->attendee_uid = cal_strdup((const char*)temp);
187                 break;
188         case CAL_PROPERTY_ATTENDEE_GROUP:
189                 temp = sqlite3_column_text(stmt, *stmt_count);
190                 attendee->attendee_group = cal_strdup((const char*)temp);
191                 break;
192         case CAL_PROPERTY_ATTENDEE_EMAIL:
193                 temp = sqlite3_column_text(stmt, *stmt_count);
194                 attendee->attendee_email = cal_strdup((const char*)temp);
195                 break;
196         case CAL_PROPERTY_ATTENDEE_ROLE:
197                 attendee->attendee_role = sqlite3_column_int(stmt, *stmt_count);
198                 break;
199         case CAL_PROPERTY_ATTENDEE_STATUS:
200                 attendee->attendee_status = sqlite3_column_int(stmt, *stmt_count);
201                 break;
202         case CAL_PROPERTY_ATTENDEE_RSVP:
203                 attendee->attendee_rsvp = sqlite3_column_int(stmt, *stmt_count);
204                 break;
205         case CAL_PROPERTY_ATTENDEE_DELEGATEE_URI:
206                 temp = sqlite3_column_text(stmt, *stmt_count);
207                 attendee->attendee_delegatee_uri = cal_strdup((const char*)temp);
208                 break;
209         case CAL_PROPERTY_ATTENDEE_DELEGATOR_URI:
210                 temp = sqlite3_column_text(stmt, *stmt_count);
211                 attendee->attendee_delegator_uri = cal_strdup((const char*)temp);
212                 break;
213         case CAL_PROPERTY_ATTENDEE_NAME:
214                 temp = sqlite3_column_text(stmt, *stmt_count);
215                 attendee->attendee_name = cal_strdup((const char*)temp);
216                 break;
217         case CAL_PROPERTY_ATTENDEE_MEMBER:
218                 temp = sqlite3_column_text(stmt, *stmt_count);
219                 attendee->attendee_member = cal_strdup((const char*)temp);
220                 break;
221         case CAL_PROPERTY_ATTENDEE_PARENT_ID:
222                 attendee->parent_id = sqlite3_column_int(stmt, *stmt_count);
223                 break;
224         default:
225                 sqlite3_column_int(stmt, *stmt_count);
226                 break;
227         }
228         *stmt_count = *stmt_count+1;
229 }
230
231 static void _cal_db_attendee_get_projection_stmt(sqlite3_stmt *stmt,
232                 const unsigned int *projection, const int projection_count,
233                 calendar_record_h record)
234 {
235         int i = 0;
236         int stmt_count = 0;
237
238         for (i = 0; i < projection_count; i++)
239                 _cal_db_attendee_get_property_stmt(stmt, projection[i], &stmt_count, record);
240 }
241
242 static int _cal_db_attendee_get_records_with_query(calendar_query_h query, int offset, int limit, calendar_list_h* out_list)
243 {
244         cal_query_s *que = NULL;
245         int ret = CALENDAR_ERROR_NONE;
246         char *condition = NULL;
247         char *projection = NULL;
248         char *order = NULL;
249         GSList *bind_text = NULL, *cursor = NULL;
250         char *query_str = NULL;
251         sqlite3_stmt *stmt = NULL;
252         int i = 0;
253         char *table_name;
254
255         que = (cal_query_s *)query;
256
257         if (CAL_STRING_EQUAL == strcmp(que->view_uri, CALENDAR_VIEW_ATTENDEE)) {
258                 table_name = cal_strdup(CAL_TABLE_ATTENDEE);
259         } else {
260                 /* LCOV_EXCL_START */
261                 ERR("uri(%s) not support get records with query", que->view_uri);
262                 return CALENDAR_ERROR_INVALID_PARAMETER;
263                 /* LCOV_EXCL_STOP */
264         }
265
266         /* make filter */
267         if (que->filter) {
268                 ret = cal_db_query_create_condition(query, &condition, &bind_text);
269                 if (CALENDAR_ERROR_NONE != ret) {
270                         /* LCOV_EXCL_START */
271                         ERR("cal_db_query_create_condition() Fail(%d), ret");
272                         CAL_FREE(table_name);
273                         return ret;
274                         /* LCOV_EXCL_STOP */
275                 }
276         }
277
278         /* make projection */
279         ret = cal_db_query_create_projection(query, &projection);
280
281         /* query: projection */
282         if (projection) {
283                 cal_db_append_string(&query_str, "SELECT");
284                 cal_db_append_string(&query_str, projection);
285                 cal_db_append_string(&query_str, "FROM");
286                 cal_db_append_string(&query_str, table_name);
287                 CAL_FREE(projection);
288         } else {
289                 cal_db_append_string(&query_str, "SELECT *, rowid FROM ");
290                 cal_db_append_string(&query_str, table_name);
291         }
292         CAL_FREE(table_name);
293
294         /* query: condition */
295         if (condition) {
296                 cal_db_append_string(&query_str, "WHERE");
297                 cal_db_append_string(&query_str, condition);
298                 CAL_FREE(condition);
299         }
300
301         /* order */
302         ret = cal_db_query_create_order(query, condition, &order);
303         if (order) {
304                 cal_db_append_string(&query_str, order);
305                 CAL_FREE(order);
306         }
307
308         char buf[CAL_STR_SHORT_LEN32] = {0};
309         if (0 < limit) {
310                 snprintf(buf, sizeof(buf), "LIMIT %d", limit);
311                 cal_db_append_string(&query_str, buf);
312                 if (0 < offset) {
313                         snprintf(buf, sizeof(buf), "OFFSET %d", offset);
314                         cal_db_append_string(&query_str, buf);
315                 }
316         }
317
318         /* query */
319         ret = cal_db_util_query_prepare(query_str, &stmt);
320         if (CALENDAR_ERROR_NONE != ret) {
321                 /* LCOV_EXCL_START */
322                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
323                 SECURE("query[%s]", query_str);
324                 if (bind_text) {
325                         g_slist_free_full(bind_text, free);
326                         bind_text = NULL;
327                 }
328                 free(query_str);
329                 return ret;
330                 /* LCOV_EXCL_STOP */
331         }
332
333         /* bind text */
334         if (bind_text)  {
335                 for (cursor = bind_text, i = 1; cursor; cursor = cursor->next, i++)
336                         cal_db_util_stmt_bind_text(stmt, i, cursor->data);
337         }
338
339         ret = calendar_list_create(out_list);
340         if (CALENDAR_ERROR_NONE != ret) {
341                 /* LCOV_EXCL_START */
342                 ERR("calendar_list_create() Fail");
343                 if (bind_text) {
344                         g_slist_free_full(bind_text, free);
345                         bind_text = NULL;
346                 }
347                 CAL_FREE(query_str);
348                 sqlite3_finalize(stmt);
349                 return ret;
350                 /* LCOV_EXCL_STOP */
351         }
352
353         while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
354                 calendar_record_h record;
355                 ret = calendar_record_create(_calendar_attendee._uri, &record);
356                 if (CALENDAR_ERROR_NONE != ret) {
357                         calendar_list_destroy(*out_list, true);
358                         *out_list = NULL;
359
360                         if (bind_text) {
361                                 g_slist_free_full(bind_text, free);
362                                 bind_text = NULL;
363                         }
364                         CAL_FREE(query_str);
365                         sqlite3_finalize(stmt);
366                         return ret;
367                 }
368                 if (0 < que->projection_count) {
369                         cal_record_set_projection(record,
370                                         que->projection, que->projection_count, que->property_count);
371
372                         _cal_db_attendee_get_projection_stmt(stmt,
373                                         que->projection, que->projection_count,
374                                         record);
375                 } else {
376                         _cal_db_attendee_get_stmt(stmt, record);
377                 }
378
379                 ret = calendar_list_add(*out_list, record);
380                 if (CALENDAR_ERROR_NONE != ret) {
381                         calendar_list_destroy(*out_list, true);
382                         *out_list = NULL;
383                         calendar_record_destroy(record, true);
384
385                         if (bind_text) {
386                                 g_slist_free_full(bind_text, free);
387                                 bind_text = NULL;
388                         }
389                         CAL_FREE(query_str);
390                         sqlite3_finalize(stmt);
391                         return ret;
392                 }
393         }
394
395         if (bind_text) {
396                 g_slist_free_full(bind_text, free);
397                 bind_text = NULL;
398         }
399         CAL_FREE(query_str);
400         sqlite3_finalize(stmt);
401
402         return CALENDAR_ERROR_NONE;
403 }
404
405 static int _cal_db_attendee_get_count(int *out_count)
406 {
407         int ret;
408         char query[CAL_DB_SQL_MAX_LEN] = {0};
409         int count = 0;
410
411         RETV_IF(NULL == out_count, CALENDAR_ERROR_INVALID_PARAMETER);
412
413         snprintf(query, sizeof(query), "SELECT count(*) FROM %s ", CAL_TABLE_ATTENDEE);
414
415         ret = cal_db_util_query_get_first_int_result(query, NULL, &count);
416         if (CALENDAR_ERROR_NONE != ret) {
417                 /* LCOV_EXCL_START */
418                 ERR("cal_db_util_query_get_first_int_result() failed");
419                 return ret;
420                 /* LCOV_EXCL_STOP */
421         }
422         DBG("%s=%d", query, count);
423
424         *out_count = count;
425         return CALENDAR_ERROR_NONE;
426 }
427
428 static int _cal_db_attendee_get_count_with_query(calendar_query_h query, int *out_count)
429 {
430         cal_query_s *que = NULL;
431         int ret = CALENDAR_ERROR_NONE;
432         char *condition = NULL;
433         char *query_str = NULL;
434         char *table_name;
435         int count = 0;
436         GSList *bind_text = NULL;
437
438         que = (cal_query_s *)query;
439
440         if (CAL_STRING_EQUAL == strcmp(que->view_uri, CALENDAR_VIEW_ATTENDEE)) {
441                 table_name = cal_strdup(CAL_TABLE_ATTENDEE);
442         } else {
443                 /* LCOV_EXCL_START */
444                 ERR("uri(%s) not support get records with query", que->view_uri);
445                 return CALENDAR_ERROR_INVALID_PARAMETER;
446                 /* LCOV_EXCL_STOP */
447         }
448
449         /* make filter */
450         if (que->filter) {
451                 ret = cal_db_query_create_condition(query, &condition, &bind_text);
452                 if (CALENDAR_ERROR_NONE != ret) {
453                         /* LCOV_EXCL_START */
454                         ERR("cal_db_query_create_condition() Fail(%d), ret");
455                         CAL_FREE(table_name);
456                         return ret;
457                         /* LCOV_EXCL_STOP */
458                 }
459         }
460
461         /* query: select */
462         cal_db_append_string(&query_str, "SELECT count(*) FROM");
463         cal_db_append_string(&query_str, table_name);
464         CAL_FREE(table_name);
465
466         CAL_FREE(table_name);
467
468         /* query: condition */
469         if (condition) {
470                 cal_db_append_string(&query_str, "WHERE");
471                 cal_db_append_string(&query_str, condition);
472                 CAL_FREE(condition);
473         }
474
475         /* query */
476         ret = cal_db_util_query_get_first_int_result(query_str, bind_text, &count);
477         if (CALENDAR_ERROR_NONE != ret) {
478                 /* LCOV_EXCL_START */
479                 ERR("cal_db_util_query_get_first_int_result() failed");
480                 if (bind_text) {
481                         g_slist_free_full(bind_text, free);
482                         bind_text = NULL;
483                 }
484                 CAL_FREE(query_str);
485                 return ret;
486                 /* LCOV_EXCL_STOP */
487         }
488         DBG("%s=%d", query_str, count);
489
490         *out_count = count;
491
492         if (bind_text) {
493                 g_slist_free_full(bind_text, free);
494                 bind_text = NULL;
495         }
496         CAL_FREE(query_str);
497         return CALENDAR_ERROR_NONE;
498 }
499