4 * Copyright (c) 2012 - 2015 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"
26 #include "cal_query.h"
27 #include "cal_utils.h"
29 static bool _cal_query_property_check(const cal_property_info_s *properties,
30 int count, unsigned int property_id)
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)
42 API int calendar_query_create(const char* view_uri, calendar_query_h* out_query)
46 RETV_IF(NULL == view_uri, CALENDAR_ERROR_INVALID_PARAMETER);
47 RETV_IF(NULL == out_query, CALENDAR_ERROR_INVALID_PARAMETER);
49 query = calloc(1, sizeof(cal_query_s));
50 RETV_IF(NULL == query, CALENDAR_ERROR_OUT_OF_MEMORY);
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;
56 return CALENDAR_ERROR_NONE;
59 API int calendar_query_set_projection(calendar_query_h query, unsigned int property_ids[], int count)
61 cal_query_s *que = NULL;
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);
69 que = (cal_query_s *)query;
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);
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 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;
88 return CALENDAR_ERROR_NONE;
91 API int calendar_query_set_distinct(calendar_query_h query, bool set)
93 cal_query_s *que = NULL;
95 RETV_IF(NULL == query, CALENDAR_ERROR_INVALID_PARAMETER);
96 que = (cal_query_s *)query;
100 return CALENDAR_ERROR_NONE;
103 API int calendar_query_set_filter(calendar_query_h query, calendar_filter_h filter)
106 calendar_filter_h new_filter;
107 int ret = CALENDAR_ERROR_NONE;
109 RETV_IF(NULL == query, CALENDAR_ERROR_INVALID_PARAMETER);
110 RETV_IF(NULL == filter, CALENDAR_ERROR_INVALID_PARAMETER);
112 que = (cal_query_s *)query;
114 if (NULL == ((cal_composite_filter_s*)filter)->filters) {
116 return CALENDAR_ERROR_NO_DATA;
119 ret = cal_filter_clone(filter, &new_filter);
120 RETV_IF(ret != CALENDAR_ERROR_NONE, ret);
123 calendar_filter_destroy((calendar_filter_h)que->filter);
125 que->filter = (cal_composite_filter_s*)new_filter;
130 API int calendar_query_set_sort(calendar_query_h query, unsigned int property_id, bool asc)
135 RETV_IF(NULL == query, CALENDAR_ERROR_INVALID_PARAMETER);
136 que = (cal_query_s *)query;
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);
143 que->sort_property_id = property_id;
146 return CALENDAR_ERROR_NONE;
149 API int calendar_query_destroy(calendar_query_h query)
153 RETV_IF(NULL == query, CALENDAR_ERROR_INVALID_PARAMETER);
154 que = (cal_query_s *)query;
157 calendar_filter_destroy((calendar_filter_h)que->filter);
159 CAL_FREE(que->view_uri);
160 CAL_FREE(que->projection);
163 return CALENDAR_ERROR_NONE;