7c1af93d87c488104a38baaa706f34659cf6525b
[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                 ERR("Empty filter");
116                 return CALENDAR_ERROR_NO_DATA;
117         }
118
119         ret = cal_filter_clone(filter, &new_filter);
120         RETV_IF(ret != CALENDAR_ERROR_NONE, ret);
121
122         if (que->filter)
123                 calendar_filter_destroy((calendar_filter_h)que->filter);
124
125         que->filter = (cal_composite_filter_s*)new_filter;
126
127         return ret;
128 }
129
130 API int calendar_query_set_sort(calendar_query_h query, unsigned int property_id, bool asc)
131 {
132         cal_query_s *que;
133         bool find = false;
134
135         RETV_IF(NULL == query, CALENDAR_ERROR_INVALID_PARAMETER);
136         que = (cal_query_s *)query;
137
138
139         find = _cal_query_property_check(que->properties, que->property_count, property_id);
140         RETVM_IF(false == find, CALENDAR_ERROR_INVALID_PARAMETER,
141                         "Invalid paramter : property_id(%d) is not supported on view_uri(%s)", property_id, que->view_uri);
142
143         que->sort_property_id = property_id;
144         que->asc = asc;
145
146         return CALENDAR_ERROR_NONE;
147 }
148
149 API int calendar_query_destroy(calendar_query_h query)
150 {
151         cal_query_s *que;
152
153         RETV_IF(NULL == query, CALENDAR_ERROR_INVALID_PARAMETER);
154         que = (cal_query_s *)query;
155
156         if (que->filter)
157                 calendar_filter_destroy((calendar_filter_h)que->filter);
158
159         CAL_FREE(que->view_uri);
160         CAL_FREE(que->projection);
161         CAL_FREE(que);
162
163         return CALENDAR_ERROR_NONE;
164 }