4 * Copyright (c) 2010 - 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.
21 #include "ctsvc_internal.h"
22 #include "ctsvc_filter.h"
27 } query_property_type_e;
29 static bool __ctsvc_query_property_check(const property_info_s *properties,
30 int count, query_property_type_e property_type, unsigned int property_id)
34 for (i = 0; i < count; i++) {
35 property_info_s *p = (property_info_s*)&(properties[i]);
36 if (property_id == p->property_id) {
37 if (property_type == QUERY_PROJECTION) {
38 if (p->property_type == CTSVC_SEARCH_PROPERTY_ALL ||
39 p->property_type == CTSVC_SEARCH_PROPERTY_PROJECTION)
51 EXPORT_API int contacts_query_create(const char *view_uri, contacts_query_h *out_query)
55 RETV_IF(NULL == out_query, CONTACTS_ERROR_INVALID_PARAMETER);
58 RETV_IF(NULL == view_uri || NULL == out_query, CONTACTS_ERROR_INVALID_PARAMETER);
60 query = calloc(1, sizeof(ctsvc_query_s));
61 RETV_IF(NULL == query, CONTACTS_ERROR_OUT_OF_MEMORY);
63 #ifdef _CONTACTS_IPC_CLIENT
64 ctsvc_view_uri_init();
67 query->view_uri = strdup(view_uri);
68 query->properties = (property_info_s*)ctsvc_view_get_all_property_infos(view_uri, &query->property_count);
69 *out_query = (contacts_query_h)query;
71 return CONTACTS_ERROR_NONE;
74 EXPORT_API int contacts_query_set_projection(contacts_query_h query, unsigned int property_ids[], int count)
76 ctsvc_query_s *query_s;
80 RETV_IF(NULL == query, CONTACTS_ERROR_INVALID_PARAMETER);
81 query_s = (ctsvc_query_s*)query;
83 for (i = 0; i < count; i++) {
84 find = __ctsvc_query_property_check(query_s->properties, query_s->property_count,
85 QUERY_PROJECTION, property_ids[i]);
86 RETVM_IF(false == find, CONTACTS_ERROR_INVALID_PARAMETER,
87 "property_id(%d) is not supported on view_uri(%s)", property_ids[i], query_s->view_uri);
89 if (query_s->projection)
90 free(query_s->projection);
92 query_s->projection = calloc(count, sizeof(unsigned int));
93 if (NULL == query_s->projection) {
96 return CONTACTS_ERROR_OUT_OF_MEMORY;
99 memcpy(query_s->projection, property_ids, sizeof(unsigned int) * count);
100 query_s->projection_count = count;
102 return CONTACTS_ERROR_NONE;
105 EXPORT_API int contacts_query_set_filter(contacts_query_h query, contacts_filter_h filter)
108 ctsvc_query_s *s_query;
109 contacts_filter_h new_filter;
111 RETV_IF(NULL == query || NULL == filter, CONTACTS_ERROR_INVALID_PARAMETER);
112 s_query = (ctsvc_query_s*)query;
114 ret = ctsvc_filter_clone(filter, &new_filter);
115 RETVM_IF(ret != CONTACTS_ERROR_NONE, ret, "ctsvc_filter_clone Fail(%d)", ret);
116 s_query->filter = (ctsvc_composite_filter_s*)new_filter;
118 return CONTACTS_ERROR_NONE;
121 EXPORT_API int contacts_query_set_sort(contacts_query_h query, unsigned int property_id, bool asc)
123 ctsvc_query_s *query_s;
126 RETV_IF(NULL == query, CONTACTS_ERROR_INVALID_PARAMETER);
127 query_s = (ctsvc_query_s*)query;
129 find = __ctsvc_query_property_check(query_s->properties, query_s->property_count, QUERY_SORTKEY, property_id);
130 RETVM_IF(false == find, CONTACTS_ERROR_INVALID_PARAMETER,
131 "property_id(%d) is not supported on view_uri(%s)", property_id, query_s->view_uri);
132 query_s->sort_property_id = property_id;
133 query_s->sort_asc = asc;
135 return CONTACTS_ERROR_NONE;
138 EXPORT_API int contacts_query_destroy(contacts_query_h query)
140 ctsvc_query_s *s_query;
141 RETV_IF(NULL == query, CONTACTS_ERROR_INVALID_PARAMETER);
142 s_query = (ctsvc_query_s*)query;
144 #ifdef _CONTACTS_IPC_CLIENT
145 ctsvc_view_uri_deinit();
149 contacts_filter_destroy((contacts_filter_h)s_query->filter);
151 free(s_query->projection);
152 free(s_query->view_uri);
155 return CONTACTS_ERROR_NONE;
158 EXPORT_API int contacts_query_set_distinct(contacts_query_h query, bool set)
160 ctsvc_query_s *query_s;
162 RETV_IF(NULL == query, CONTACTS_ERROR_INVALID_PARAMETER);
163 query_s = (ctsvc_query_s*)query;
164 query_s->distinct = set;
166 return CONTACTS_ERROR_NONE;