add comment LCOV_EXCL
[platform/core/pim/calendar-service.git] / common / cal_query.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_filter.h"
26 #include "cal_query.h"
27 #include "cal_utils.h"
28
29 static bool _cal_query_property_check(const cal_property_info_s *properties,
30                 int count, unsigned int property_id)
31 {
32         int i;
33
34         for (i = 0; i < count; i++) {
35                 cal_property_info_s *p = (cal_property_info_s*)&(properties[i]);
36                 if (property_id == p->property_id)
37                         return true;
38         }
39         return false;
40 }
41
42 API int calendar_query_create(const char* view_uri, calendar_query_h* out_query)
43 {
44         cal_query_s *query;
45
46         RETV_IF(NULL == view_uri, CALENDAR_ERROR_INVALID_PARAMETER);
47         RETV_IF(NULL == out_query, CALENDAR_ERROR_INVALID_PARAMETER);
48
49         query = calloc(1, sizeof(cal_query_s));
50         RETV_IF(NULL == query, CALENDAR_ERROR_OUT_OF_MEMORY);
51
52         query->view_uri = cal_strdup(view_uri);
53         query->properties = (cal_property_info_s *)cal_view_get_property_info(view_uri, &query->property_count);
54         *out_query = (calendar_query_h)query;
55
56         return CALENDAR_ERROR_NONE;
57 }
58
59 API int calendar_query_set_projection(calendar_query_h query, unsigned int property_ids[], int count)
60 {
61         cal_query_s *que = NULL;
62         int i;
63         bool find;
64
65         RETV_IF(NULL == query, CALENDAR_ERROR_INVALID_PARAMETER);
66         RETV_IF(NULL == property_ids, CALENDAR_ERROR_INVALID_PARAMETER);
67         RETVM_IF(count < 0, CALENDAR_ERROR_INVALID_PARAMETER, "count(%d) < 0", count);
68
69         que = (cal_query_s *)query;
70
71         for (i = 0; i < count; i++) {
72                 find = _cal_query_property_check(que->properties, que->property_count, property_ids[i]);
73                 RETVM_IF(false == find, CALENDAR_ERROR_INVALID_PARAMETER,
74                                 "Invalid parameter : property_id(%d) is not supported on view_uri(%s)", property_ids[i], que->view_uri);
75
76                 find = CAL_PROPERTY_CHECK_FLAGS(property_ids[i], CAL_PROPERTY_FLAGS_FILTER);
77                 RETVM_IF(true == find, CALENDAR_ERROR_INVALID_PARAMETER,
78                                 "Invalid parameter : property_id(%d) is not supported on view_uri(%s)", property_ids[i], que->view_uri);
79         }
80
81         CAL_FREE(que->projection);
82
83         que->projection = calloc(count, sizeof(unsigned int));
84         RETVM_IF(NULL == que->projection, CALENDAR_ERROR_OUT_OF_MEMORY, "calloc() Fail");
85         memcpy(que->projection, property_ids, sizeof(unsigned int) * count);
86         que->projection_count = count;
87
88         return CALENDAR_ERROR_NONE;
89 }
90
91 API int calendar_query_set_distinct(calendar_query_h query, bool set)
92 {
93         cal_query_s *que = NULL;
94
95         RETV_IF(NULL == query, CALENDAR_ERROR_INVALID_PARAMETER);
96         que = (cal_query_s *)query;
97
98         que->distinct = set;
99
100         return CALENDAR_ERROR_NONE;
101 }
102
103 API int calendar_query_set_filter(calendar_query_h query, calendar_filter_h filter)
104 {
105         cal_query_s *que;
106         calendar_filter_h new_filter;
107         int ret = CALENDAR_ERROR_NONE;
108
109         RETV_IF(NULL == query, CALENDAR_ERROR_INVALID_PARAMETER);
110         RETV_IF(NULL == filter, CALENDAR_ERROR_INVALID_PARAMETER);
111
112         que = (cal_query_s *)query;
113
114         if (NULL == ((cal_composite_filter_s*)filter)->filters) {
115                 /* LCOV_EXCL_START */
116                 ERR("Empty filter");
117                 return CALENDAR_ERROR_NO_DATA;
118                 /* LCOV_EXCL_STOP */
119         }
120
121         ret = cal_filter_clone(filter, &new_filter);
122         RETV_IF(ret != CALENDAR_ERROR_NONE, ret);
123
124         if (que->filter)
125                 calendar_filter_destroy((calendar_filter_h)que->filter);
126
127         que->filter = (cal_composite_filter_s*)new_filter;
128
129         return ret;
130 }
131
132 API int calendar_query_set_sort(calendar_query_h query, unsigned int property_id, bool asc)
133 {
134         cal_query_s *que;
135         bool find = false;
136
137         RETV_IF(NULL == query, CALENDAR_ERROR_INVALID_PARAMETER);
138         que = (cal_query_s *)query;
139
140
141         find = _cal_query_property_check(que->properties, que->property_count, property_id);
142         RETVM_IF(false == find, CALENDAR_ERROR_INVALID_PARAMETER,
143                         "Invalid paramter : property_id(%d) is not supported on view_uri(%s)", property_id, que->view_uri);
144
145         que->sort_property_id = property_id;
146         que->asc = asc;
147
148         return CALENDAR_ERROR_NONE;
149 }
150
151 API int calendar_query_destroy(calendar_query_h query)
152 {
153         cal_query_s *que;
154
155         RETV_IF(NULL == query, CALENDAR_ERROR_INVALID_PARAMETER);
156         que = (cal_query_s *)query;
157
158         if (que->filter)
159                 calendar_filter_destroy((calendar_filter_h)que->filter);
160
161         CAL_FREE(que->view_uri);
162         CAL_FREE(que->projection);
163         CAL_FREE(que);
164
165         return CALENDAR_ERROR_NONE;
166 }