4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include "cal_internal.h"
23 #include "cal_typedef.h"
25 #include "cal_filter.h"
27 #include "cal_query.h"
29 static bool __cal_query_property_check(const cal_property_info_s *properties,
30 int count, unsigned int property_id);
32 static bool __cal_query_property_check(const cal_property_info_s *properties,
33 int count, unsigned int property_id)
39 cal_property_info_s *p = (cal_property_info_s*)&(properties[i]);
40 if (property_id == p->property_id) {
47 API int calendar_query_create( const char* view_uri, calendar_query_h* out_query )
51 retv_if(NULL == view_uri || NULL == out_query, CALENDAR_ERROR_INVALID_PARAMETER);
53 query = (cal_query_s *)calloc(1, sizeof(cal_query_s));
54 query->view_uri = strdup(view_uri);
55 query->properties = (cal_property_info_s *)_cal_view_get_property_info(view_uri, &query->property_count);
56 *out_query = (calendar_query_h)query;
58 return CALENDAR_ERROR_NONE;
61 API int calendar_query_set_projection(calendar_query_h query, unsigned int property_ids[], int count)
63 cal_query_s *que = NULL;
67 retv_if(NULL == query, CALENDAR_ERROR_INVALID_PARAMETER);
68 que = (cal_query_s *)query;
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);
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);
81 CAL_FREE(que->projection);
83 que->projection = calloc(count, sizeof(unsigned int));
84 memcpy(que->projection, property_ids, sizeof(unsigned int) * count);
85 que->projection_count = count;
87 return CALENDAR_ERROR_NONE;
90 API int calendar_query_set_distinct(calendar_query_h query, bool set)
92 cal_query_s *que = NULL;
94 retv_if(NULL == query, CALENDAR_ERROR_INVALID_PARAMETER);
95 que = (cal_query_s *)query;
99 return CALENDAR_ERROR_NONE;
102 API int calendar_query_set_filter(calendar_query_h query, calendar_filter_h filter)
105 calendar_filter_h new_filter;
106 int ret = CALENDAR_ERROR_NONE;
108 retv_if(NULL == query || NULL == filter, CALENDAR_ERROR_INVALID_PARAMETER);
110 que = (cal_query_s *)query;
112 if(((cal_composite_filter_s*)filter)->filters == NULL)
115 return CALENDAR_ERROR_NO_DATA;
118 ret = _cal_filter_clone(filter,&new_filter);
119 retv_if(ret!=CALENDAR_ERROR_NONE, ret);
123 calendar_filter_destroy((calendar_filter_h)que->filter);
126 que->filter = (cal_composite_filter_s*)new_filter;
131 API int calendar_query_set_sort(calendar_query_h query, unsigned int property_id, bool asc)
136 retv_if(NULL == query, CALENDAR_ERROR_INVALID_PARAMETER);
137 que = (cal_query_s *)query;
140 find = __cal_query_property_check(que->properties, que->property_count, property_id);
141 retvm_if(false == find, CALENDAR_ERROR_INVALID_PARAMETER,
142 "Invalid paramter : property_id(%d) is not supported on view_uri(%s)", property_id, que->view_uri);
144 que->sort_property_id = property_id;
147 return CALENDAR_ERROR_NONE;
150 API int calendar_query_destroy( calendar_query_h query )
154 retv_if(NULL == query, CALENDAR_ERROR_INVALID_PARAMETER);
155 que = (cal_query_s *)query;
159 calendar_filter_destroy((calendar_filter_h)que->filter);
162 CAL_FREE(que->view_uri);
163 CAL_FREE(que->projection);
166 return CALENDAR_ERROR_NONE;
169 int _cal_query_clone(calendar_query_h query, calendar_query_h* out_query)
172 cal_query_s *out_que;
173 cal_filter_s *out_filter = NULL;
174 int ret = CALENDAR_ERROR_NONE;
176 retv_if(NULL == query, CALENDAR_ERROR_INVALID_PARAMETER);
177 que = (cal_query_s *)query;
179 ret = calendar_query_create(que->view_uri, out_query);
180 retv_if(CALENDAR_ERROR_NONE != ret, CALENDAR_ERROR_OUT_OF_MEMORY);
181 out_que = (cal_query_s *)*out_query;
185 _cal_filter_clone((calendar_filter_h)que->filter,(calendar_filter_h*)&out_filter);
188 if (que->projection_count > 0)
190 out_que->projection = calloc(que->projection_count, sizeof(unsigned int));
191 memcpy(out_que->projection, que->projection , sizeof(unsigned int) * que->projection_count);
192 out_que->projection_count = que->projection_count;
194 out_que->sort_property_id = que->sort_property_id;
195 out_que->asc = que->asc;
197 return CALENDAR_ERROR_NONE;