ctsvc_client_handle_remove in CTS_MUTEX_CONNECTION during disconnect_on_thread
[platform/core/pim/contacts-service.git] / client / ctsvc_client_db.c
1 /*
2  * Contacts Service
3  *
4  * Copyright (c) 2010 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Dohyung Jin <dh.jin@samsung.com>
7  *                 Jongwon Lee <gogosing.lee@samsung.com>
8  *                 Donghee Ye <donghee.ye@samsung.com>
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */
23
24 #include "contacts.h"
25 #include "ctsvc_internal.h"
26 #include "ctsvc_client_handle.h"
27 #include "ctsvc_client_db_helper.h"
28
29 API int contacts_db_insert_record(contacts_record_h record, int *id)
30 {
31         int ret;
32         contacts_h contact = NULL;
33
34         ret = ctsvc_client_handle_get_p(&contact);
35         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_get_p() Fail(%d)", ret);
36
37         ret = ctsvc_client_db_insert_record(contact, record, id);
38
39         return ret;
40 }
41
42 API int contacts_db_get_record(const char* view_uri, int id, contacts_record_h* out_record)
43 {
44         int ret;
45         contacts_h contact = NULL;
46
47         ret = ctsvc_client_handle_get_p(&contact);
48         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_get_p() Fail(%d)", ret);
49
50         ret = ctsvc_client_db_get_record(contact, view_uri, id, out_record);
51
52         return ret;
53 }
54
55 API int contacts_db_update_record(contacts_record_h record)
56 {
57         int ret;
58         contacts_h contact = NULL;
59
60         ret = ctsvc_client_handle_get_p(&contact);
61         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_get_p() Fail(%d)", ret);
62
63         ret = ctsvc_client_db_update_record(contact, record);
64
65         return ret;
66 }
67
68 API int contacts_db_delete_record(const char* view_uri, int id)
69 {
70         int ret;
71         contacts_h contact = NULL;
72
73         ret = ctsvc_client_handle_get_p(&contact);
74         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_get_p() Fail(%d)", ret);
75
76         ret = ctsvc_client_db_delete_record(contact, view_uri, id);
77
78         return ret;
79 }
80
81 API int contacts_db_replace_record(contacts_record_h record, int id)
82 {
83         int ret;
84         contacts_h contact = NULL;
85
86         ret = ctsvc_client_handle_get_p(&contact);
87         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_get_p() Fail(%d)", ret);
88
89         ret = ctsvc_client_db_replace_record(contact, record, id);
90
91         return ret;
92
93 }
94
95 API int contacts_db_get_all_records(const char* view_uri, int offset, int limit, contacts_list_h* out_list)
96 {
97         int ret;
98         contacts_h contact = NULL;
99
100         ret = ctsvc_client_handle_get_p(&contact);
101         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_get_p() Fail(%d)", ret);
102
103         ret = ctsvc_client_db_get_all_records(contact, view_uri, offset, limit, out_list);
104
105         return ret;
106 }
107
108 API int contacts_db_get_records_with_query(contacts_query_h query, int offset, int limit, contacts_list_h* out_list)
109 {
110         int ret;
111         contacts_h contact = NULL;
112
113         ret = ctsvc_client_handle_get_p(&contact);
114         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_get_p() Fail(%d)", ret);
115
116         ret = ctsvc_client_db_get_records_with_query(contact, query, offset, limit, out_list);
117
118         return ret;
119 }
120
121
122 API int contacts_db_get_count(const char* view_uri, int *out_count)
123 {
124         int ret;
125         contacts_h contact = NULL;
126
127         ret = ctsvc_client_handle_get_p(&contact);
128         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_get_p() Fail(%d)", ret);
129
130         ret = ctsvc_client_db_get_count(contact, view_uri, out_count);
131
132         return ret;
133 }
134
135 API int contacts_db_get_count_with_query(contacts_query_h query, int *out_count)
136 {
137         int ret;
138         contacts_h contact = NULL;
139
140         ret = ctsvc_client_handle_get_p(&contact);
141         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_get_p() Fail(%d)", ret);
142
143         ret = ctsvc_client_db_get_count_with_query(contact, query, out_count);
144
145         return ret;
146 }
147
148 API int contacts_db_insert_records(contacts_list_h list, int **ids, int *count)
149 {
150         int ret;
151         contacts_h contact = NULL;
152
153         ret = ctsvc_client_handle_get_p(&contact);
154         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_get_p() Fail(%d)", ret);
155
156         ret = ctsvc_client_db_insert_records(contact, list, ids, count);
157
158         return ret;
159 }
160
161 API int contacts_db_update_records(contacts_list_h list)
162 {
163         int ret;
164         contacts_h contact = NULL;
165
166         ret = ctsvc_client_handle_get_p(&contact);
167         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_get_p() Fail(%d)", ret);
168
169         ret = ctsvc_client_db_update_records(contact, list);
170
171         return ret;
172 }
173
174 API int contacts_db_delete_records(const char* view_uri, int ids[], int count)
175 {
176         int ret;
177         contacts_h contact = NULL;
178
179         ret = ctsvc_client_handle_get_p(&contact);
180         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_get_p() Fail(%d)", ret);
181
182         ret = ctsvc_client_db_delete_records(contact, view_uri, ids, count);
183
184         return ret;
185 }
186
187 API int contacts_db_replace_records(contacts_list_h list, int ids[], int count)
188 {
189         int ret;
190         contacts_h contact = NULL;
191
192         ret = ctsvc_client_handle_get_p(&contact);
193         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_get_p() Fail(%d)", ret);
194
195         ret = ctsvc_client_db_replace_records(contact, list, ids, count);
196
197         return ret;
198 }
199
200 API int contacts_db_get_changes_by_version(const char* view_uri, int addressbook_id,
201                 int contacts_db_version, contacts_list_h* record_list, int* current_contacts_db_version)
202 {
203         int ret;
204         contacts_h contact = NULL;
205
206         ret = ctsvc_client_handle_get_p(&contact);
207         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_get_p() Fail(%d)", ret);
208
209         ret = ctsvc_client_db_get_changes_by_version(contact, view_uri, addressbook_id, contacts_db_version, record_list, current_contacts_db_version);
210
211         return ret;
212 }
213
214 API int contacts_db_get_current_version(int* contacts_db_version)
215 {
216         int ret;
217         contacts_h contact = NULL;
218
219         ret = ctsvc_client_handle_get_p(&contact);
220         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_get_p() Fail(%d)", ret);
221
222         ret = ctsvc_client_db_get_current_version(contact, contacts_db_version);
223
224         return ret;
225
226 }
227
228 API int contacts_db_search_records(const char* view_uri, const char *keyword,
229                 int offset, int limit, contacts_list_h* out_list)
230 {
231         int ret;
232         contacts_h contact = NULL;
233
234         ret = ctsvc_client_handle_get_p(&contact);
235         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_get_p() Fail(%d)", ret);
236
237         ret = ctsvc_client_db_search_records(contact, view_uri, keyword, offset, limit, out_list);
238
239         return ret;
240 }
241
242 API int contacts_db_search_records_with_range(const char* view_uri, const char *keyword,
243                 int offset, int limit, int range, contacts_list_h* out_list)
244 {
245         int ret;
246         contacts_h contact = NULL;
247
248         ret = ctsvc_client_handle_get_p(&contact);
249         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_get_p() Fail(%d)", ret);
250
251         ret = ctsvc_client_db_search_records_with_range(contact, view_uri, keyword, offset, limit, range, out_list);
252
253         return ret;
254 }
255
256 API int contacts_db_search_records_with_query(contacts_query_h query, const char *keyword,
257                 int offset, int limit, contacts_list_h* out_list)
258 {
259         int ret;
260         contacts_h contact = NULL;
261
262         ret = ctsvc_client_handle_get_p(&contact);
263         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_get_p() Fail(%d)", ret);
264
265         ret = ctsvc_client_db_search_records_with_query(contact, query, keyword, offset, limit, out_list);
266
267         return ret;
268 }
269
270 API int contacts_db_get_last_change_version(int* last_version)
271 {
272         int ret;
273         contacts_h contact = NULL;
274
275         ret = ctsvc_client_handle_get_p(&contact);
276         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_get_p() Fail(%d)", ret);
277
278         ret = ctsvc_client_db_get_last_change_version(contact, last_version);
279
280         return ret;
281 }
282
283 API int contacts_db_get_status(contacts_db_status_e *status)
284 {
285         int ret;
286         contacts_h contact = NULL;
287
288         ret = ctsvc_client_handle_get_p(&contact);
289         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_get_p() Fail(%d)", ret);
290
291         ret = ctsvc_client_db_get_status(contact, status);
292
293         return ret;
294 }
295
296 API int contacts_db_add_status_changed_cb(
297                 contacts_db_status_changed_cb cb, void* user_data)
298 {
299         int ret;
300         contacts_h contact = NULL;
301
302         ret = ctsvc_client_handle_get_p(&contact);
303         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_get_p() Fail(%d)", ret);
304
305         ret = ctsvc_client_db_add_status_changed_cb(contact, cb, user_data);
306
307         return ret;
308 }
309
310 API int contacts_db_remove_status_changed_cb(
311                 contacts_db_status_changed_cb cb, void* user_data)
312 {
313         int ret;
314         contacts_h contact = NULL;
315
316         ret = ctsvc_client_handle_get_p(&contact);
317         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_client_handle_get_p() Fail(%d)", ret);
318
319         ret = ctsvc_client_db_remove_status_changed_cb(contact, cb, user_data);
320
321         return ret;
322 }
323