e64df42bb0c6d3fc2b3a4bd47db02c6d84c6d561
[platform/core/pim/calendar-service.git] / server / db / cal_db_plugin_attendee.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_record.h"
26
27 #include "cal_db_util.h"
28 #include "cal_db_query.h"
29 #include "cal_db_plugin_attendee_helper.h"
30 #include "cal_utils.h"
31
32 static int _cal_db_attendee_get_all_records(int offset, int limit, calendar_list_h* out_list);
33 static int _cal_db_attendee_get_records_with_query(calendar_query_h query, int offset, int limit, calendar_list_h* out_list);
34 static int _cal_db_attendee_get_count(int *out_count);
35 static int _cal_db_attendee_get_count_with_query(calendar_query_h query, int *out_count);
36 /* static int _cal_db_attendee_get_record(int id, calendar_record_h* out_record) */
37
38 cal_db_plugin_cb_s cal_db_attendee_plugin_cb = {
39         .is_query_only = false,
40         .insert_record = NULL,
41         .update_record = NULL,
42         .replace_record = NULL,
43         .delete_record = NULL,
44         .insert_records = NULL,
45         .update_records = NULL,
46         .delete_records = NULL,
47         .replace_records = NULL,
48         .get_record = NULL,
49         .get_all_records = _cal_db_attendee_get_all_records,
50         .get_records_with_query = _cal_db_attendee_get_records_with_query,
51         .get_count = _cal_db_attendee_get_count,
52         .get_count_with_query = _cal_db_attendee_get_count_with_query
53 };
54
55 static void _cal_db_attendee_get_stmt(sqlite3_stmt *stmt, calendar_record_h record)
56 {
57         cal_attendee_s *attendee = NULL;
58         int index;
59         const unsigned char *temp;
60
61         attendee = (cal_attendee_s*)(record);
62         index = 0;
63
64         attendee->parent_id = sqlite3_column_int(stmt, index++);
65
66         temp = sqlite3_column_text(stmt, index++);
67         attendee->attendee_name = cal_strdup((const char*)temp);
68
69         temp = sqlite3_column_text(stmt, index++);
70         attendee->attendee_email = cal_strdup((const char*)temp);
71
72         temp = sqlite3_column_text(stmt, index++);
73         attendee->attendee_number = cal_strdup((const char*)temp);
74
75         attendee->attendee_status = sqlite3_column_int(stmt, index++);
76         attendee->attendee_ct_index = sqlite3_column_int(stmt, index++);
77         attendee->attendee_role = sqlite3_column_int(stmt, index++);
78         attendee->attendee_rsvp = sqlite3_column_int(stmt, index++);
79
80         temp = sqlite3_column_text(stmt, index++);
81         attendee->attendee_group = cal_strdup((const char*)temp);
82
83         temp = sqlite3_column_text(stmt, index++);
84         attendee->attendee_delegator_uri = cal_strdup((const char*)temp);
85
86         temp = sqlite3_column_text(stmt, index++);
87         attendee->attendee_uid = cal_strdup((const char*)temp);
88
89         attendee->attendee_cutype = sqlite3_column_int(stmt, index++);
90
91         temp = sqlite3_column_text(stmt, index++);
92         attendee->attendee_delegatee_uri = cal_strdup((const char*)temp);
93
94         temp = sqlite3_column_text(stmt, index++);
95         attendee->attendee_member = cal_strdup((const char*)temp);
96
97         attendee->id = sqlite3_column_int(stmt, index++);
98 }
99
100 static int _cal_db_attendee_get_all_records(int offset, int limit, calendar_list_h* out_list)
101 {
102         int ret;
103         char query[CAL_DB_SQL_MAX_LEN] = {0};
104         char offsetquery[CAL_DB_SQL_MAX_LEN] = {0};
105         char limitquery[CAL_DB_SQL_MAX_LEN] = {0};
106         sqlite3_stmt *stmt = NULL;
107
108         RETV_IF(NULL == out_list, CALENDAR_ERROR_INVALID_PARAMETER);
109
110         ret = calendar_list_create(out_list);
111         RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "calendar_list_create() Fail(%d)", ret);
112
113         if (0 < limit)
114                 snprintf(limitquery, sizeof(limitquery), "LIMIT %d ", limit);
115
116         if (0 < offset)
117                 snprintf(offsetquery, sizeof(offsetquery), "OFFSET %d ", offset);
118
119         snprintf(query, sizeof(query), "SELECT *, rowid FROM %s %s %s ",
120                         CAL_TABLE_ATTENDEE, limitquery, offsetquery);
121
122         ret = cal_db_util_query_prepare(query, &stmt);
123         if (CALENDAR_ERROR_NONE != ret) {
124                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
125                 SECURE("query[%s]", query);
126                 calendar_list_destroy(*out_list, true);
127                 *out_list = NULL;
128                 return ret;
129         }
130
131         calendar_record_h record = NULL;
132
133         while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt))   {
134                 ret = calendar_record_create(_calendar_attendee._uri, &record);
135                 if (CALENDAR_ERROR_NONE != ret) {
136                         calendar_list_destroy(*out_list, true);
137                         *out_list = NULL;
138                         sqlite3_finalize(stmt);
139                         return ret;
140                 }
141                 _cal_db_attendee_get_stmt(stmt, record);
142
143                 ret = calendar_list_add(*out_list, record);
144                 if (CALENDAR_ERROR_NONE != ret) {
145                         calendar_list_destroy(*out_list, true);
146                         *out_list = NULL;
147                         calendar_record_destroy(record, true);
148                         sqlite3_finalize(stmt);
149                         return ret;
150                 }
151         }
152         sqlite3_finalize(stmt);
153
154         return CALENDAR_ERROR_NONE;
155 }
156
157 static void _cal_db_attendee_get_property_stmt(sqlite3_stmt *stmt,
158                 unsigned int property, int *stmt_count, calendar_record_h record)
159 {
160         cal_attendee_s *attendee = NULL;
161         const unsigned char *temp;
162
163         attendee = (cal_attendee_s*)(record);
164
165         switch (property) {
166         case CAL_PROPERTY_ATTENDEE_NUMBER:
167                 temp = sqlite3_column_text(stmt, *stmt_count);
168                 attendee->attendee_number = cal_strdup((const char*)temp);
169                 break;
170         case CAL_PROPERTY_ATTENDEE_CUTYPE:
171                 attendee->attendee_cutype = sqlite3_column_int(stmt, *stmt_count);
172                 break;
173         case CAL_PROPERTY_ATTENDEE_CT_INDEX:
174                 attendee->attendee_ct_index = sqlite3_column_int(stmt, *stmt_count);
175                 break;
176         case CAL_PROPERTY_ATTENDEE_UID:
177                 temp = sqlite3_column_text(stmt, *stmt_count);
178                 attendee->attendee_uid = cal_strdup((const char*)temp);
179                 break;
180         case CAL_PROPERTY_ATTENDEE_GROUP:
181                 temp = sqlite3_column_text(stmt, *stmt_count);
182                 attendee->attendee_group = cal_strdup((const char*)temp);
183                 break;
184         case CAL_PROPERTY_ATTENDEE_EMAIL:
185                 temp = sqlite3_column_text(stmt, *stmt_count);
186                 attendee->attendee_email = cal_strdup((const char*)temp);
187                 break;
188         case CAL_PROPERTY_ATTENDEE_ROLE:
189                 attendee->attendee_role = sqlite3_column_int(stmt, *stmt_count);
190                 break;
191         case CAL_PROPERTY_ATTENDEE_STATUS:
192                 attendee->attendee_status = sqlite3_column_int(stmt, *stmt_count);
193                 break;
194         case CAL_PROPERTY_ATTENDEE_RSVP:
195                 attendee->attendee_rsvp = sqlite3_column_int(stmt, *stmt_count);
196                 break;
197         case CAL_PROPERTY_ATTENDEE_DELEGATEE_URI:
198                 temp = sqlite3_column_text(stmt, *stmt_count);
199                 attendee->attendee_delegatee_uri = cal_strdup((const char*)temp);
200                 break;
201         case CAL_PROPERTY_ATTENDEE_DELEGATOR_URI:
202                 temp = sqlite3_column_text(stmt, *stmt_count);
203                 attendee->attendee_delegator_uri = cal_strdup((const char*)temp);
204                 break;
205         case CAL_PROPERTY_ATTENDEE_NAME:
206                 temp = sqlite3_column_text(stmt, *stmt_count);
207                 attendee->attendee_name = cal_strdup((const char*)temp);
208                 break;
209         case CAL_PROPERTY_ATTENDEE_MEMBER:
210                 temp = sqlite3_column_text(stmt, *stmt_count);
211                 attendee->attendee_member = cal_strdup((const char*)temp);
212                 break;
213         case CAL_PROPERTY_ATTENDEE_PARENT_ID:
214                 attendee->parent_id = sqlite3_column_int(stmt, *stmt_count);
215                 break;
216         default:
217                 sqlite3_column_int(stmt, *stmt_count);
218                 break;
219         }
220         *stmt_count = *stmt_count+1;
221 }
222
223 static void _cal_db_attendee_get_projection_stmt(sqlite3_stmt *stmt,
224                 const unsigned int *projection, const int projection_count,
225                 calendar_record_h record)
226 {
227         int i = 0;
228         int stmt_count = 0;
229
230         for (i = 0; i < projection_count; i++)
231                 _cal_db_attendee_get_property_stmt(stmt, projection[i], &stmt_count, record);
232 }
233
234 static int _cal_db_attendee_get_records_with_query(calendar_query_h query, int offset, int limit, calendar_list_h* out_list)
235 {
236         cal_query_s *que = NULL;
237         int ret = CALENDAR_ERROR_NONE;
238         char *condition = NULL;
239         char *projection = NULL;
240         char *order = NULL;
241         GSList *bind_text = NULL, *cursor = NULL;
242         char *query_str = NULL;
243         sqlite3_stmt *stmt = NULL;
244         int i = 0;
245         char *table_name;
246
247         que = (cal_query_s *)query;
248
249         if (CAL_STRING_EQUAL == strcmp(que->view_uri, CALENDAR_VIEW_ATTENDEE)) {
250                 table_name = cal_strdup(CAL_TABLE_ATTENDEE);
251         } else {
252                 ERR("uri(%s) not support get records with query", que->view_uri);
253                 return CALENDAR_ERROR_INVALID_PARAMETER;
254         }
255
256         /* make filter */
257         if (que->filter) {
258                 ret = cal_db_query_create_condition(query, &condition, &bind_text);
259                 if (CALENDAR_ERROR_NONE != ret) {
260                         CAL_FREE(table_name);
261                         ERR("cal_db_query_create_condition() Fail(%d), ret");
262                         return ret;
263                 }
264         }
265
266         /* make projection */
267         ret = cal_db_query_create_projection(query, &projection);
268
269         /* query: projection */
270         if (projection) {
271                 cal_db_append_string(&query_str, "SELECT");
272                 cal_db_append_string(&query_str, projection);
273                 cal_db_append_string(&query_str, "FROM");
274                 cal_db_append_string(&query_str, table_name);
275                 CAL_FREE(projection);
276         } else {
277                 cal_db_append_string(&query_str, "SELECT *, rowid FROM ");
278                 cal_db_append_string(&query_str, table_name);
279         }
280         CAL_FREE(table_name);
281
282         /* query: condition */
283         if (condition) {
284                 cal_db_append_string(&query_str, "WHERE");
285                 cal_db_append_string(&query_str, condition);
286                 CAL_FREE(condition);
287         }
288
289         /* order */
290         ret = cal_db_query_create_order(query, condition, &order);
291         if (order) {
292                 cal_db_append_string(&query_str, order);
293                 CAL_FREE(order);
294         }
295
296         char buf[CAL_STR_SHORT_LEN32] = {0};
297         if (0 < limit) {
298                 snprintf(buf, sizeof(buf), "LIMIT %d", limit);
299                 cal_db_append_string(&query_str, buf);
300                 if (0 < offset) {
301                         snprintf(buf, sizeof(buf), "OFFSET %d", offset);
302                         cal_db_append_string(&query_str, buf);
303                 }
304         }
305
306         /* query */
307         ret = cal_db_util_query_prepare(query_str, &stmt);
308         if (CALENDAR_ERROR_NONE != ret) {
309                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
310                 SECURE("query[%s]", query_str);
311                 if (bind_text) {
312                         g_slist_free_full(bind_text, free);
313                         bind_text = NULL;
314                 }
315                 free(query_str);
316                 return ret;
317         }
318
319         /* bind text */
320         if (bind_text)  {
321                 for (cursor = bind_text, i = 1; cursor; cursor = cursor->next, i++)
322                         cal_db_util_stmt_bind_text(stmt, i, cursor->data);
323         }
324
325         ret = calendar_list_create(out_list);
326         if (CALENDAR_ERROR_NONE != ret) {
327                 if (bind_text) {
328                         g_slist_free_full(bind_text, free);
329                         bind_text = NULL;
330                 }
331                 CAL_FREE(query_str);
332                 ERR("calendar_list_create() Fail");
333                 sqlite3_finalize(stmt);
334                 return ret;
335         }
336
337         while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
338                 calendar_record_h record;
339                 ret = calendar_record_create(_calendar_attendee._uri, &record);
340                 if (CALENDAR_ERROR_NONE != ret) {
341                         calendar_list_destroy(*out_list, true);
342                         *out_list = NULL;
343
344                         if (bind_text) {
345                                 g_slist_free_full(bind_text, free);
346                                 bind_text = NULL;
347                         }
348                         CAL_FREE(query_str);
349                         sqlite3_finalize(stmt);
350                         return ret;
351                 }
352                 if (0 < que->projection_count) {
353                         cal_record_set_projection(record,
354                                         que->projection, que->projection_count, que->property_count);
355
356                         _cal_db_attendee_get_projection_stmt(stmt,
357                                         que->projection, que->projection_count,
358                                         record);
359                 } else {
360                         _cal_db_attendee_get_stmt(stmt, record);
361                 }
362
363                 ret = calendar_list_add(*out_list, record);
364                 if (CALENDAR_ERROR_NONE != ret) {
365                         calendar_list_destroy(*out_list, true);
366                         *out_list = NULL;
367                         calendar_record_destroy(record, true);
368
369                         if (bind_text) {
370                                 g_slist_free_full(bind_text, free);
371                                 bind_text = NULL;
372                         }
373                         CAL_FREE(query_str);
374                         sqlite3_finalize(stmt);
375                         return ret;
376                 }
377         }
378
379         if (bind_text) {
380                 g_slist_free_full(bind_text, free);
381                 bind_text = NULL;
382         }
383         CAL_FREE(query_str);
384         sqlite3_finalize(stmt);
385
386         return CALENDAR_ERROR_NONE;
387 }
388
389 static int _cal_db_attendee_get_count(int *out_count)
390 {
391         int ret;
392         char query[CAL_DB_SQL_MAX_LEN] = {0};
393         int count = 0;
394
395         RETV_IF(NULL == out_count, CALENDAR_ERROR_INVALID_PARAMETER);
396
397         snprintf(query, sizeof(query), "SELECT count(*) FROM %s ", CAL_TABLE_ATTENDEE);
398
399         ret = cal_db_util_query_get_first_int_result(query, NULL, &count);
400         if (CALENDAR_ERROR_NONE != ret) {
401                 ERR("cal_db_util_query_get_first_int_result() failed");
402                 return ret;
403         }
404         DBG("%s=%d", query, count);
405
406         *out_count = count;
407         return CALENDAR_ERROR_NONE;
408 }
409
410 static int _cal_db_attendee_get_count_with_query(calendar_query_h query, int *out_count)
411 {
412         cal_query_s *que = NULL;
413         int ret = CALENDAR_ERROR_NONE;
414         char *condition = NULL;
415         char *query_str = NULL;
416         char *table_name;
417         int count = 0;
418         GSList *bind_text = NULL;
419
420         que = (cal_query_s *)query;
421
422         if (CAL_STRING_EQUAL == strcmp(que->view_uri, CALENDAR_VIEW_ATTENDEE)) {
423                 table_name = cal_strdup(CAL_TABLE_ATTENDEE);
424         } else {
425                 ERR("uri(%s) not support get records with query", que->view_uri);
426                 return CALENDAR_ERROR_INVALID_PARAMETER;
427         }
428
429         /* make filter */
430         if (que->filter) {
431                 ret = cal_db_query_create_condition(query, &condition, &bind_text);
432                 if (CALENDAR_ERROR_NONE != ret) {
433                         CAL_FREE(table_name);
434                         ERR("cal_db_query_create_condition() Fail(%d), ret");
435                         return ret;
436                 }
437         }
438
439         /* query: select */
440         cal_db_append_string(&query_str, "SELECT count(*) FROM");
441         cal_db_append_string(&query_str, table_name);
442         CAL_FREE(table_name);
443
444         CAL_FREE(table_name);
445
446         /* query: condition */
447         if (condition) {
448                 cal_db_append_string(&query_str, "WHERE");
449                 cal_db_append_string(&query_str, condition);
450                 CAL_FREE(condition);
451         }
452
453         /* query */
454         ret = cal_db_util_query_get_first_int_result(query_str, bind_text, &count);
455         if (CALENDAR_ERROR_NONE != ret) {
456                 ERR("cal_db_util_query_get_first_int_result() failed");
457                 if (bind_text) {
458                         g_slist_free_full(bind_text, free);
459                         bind_text = NULL;
460                 }
461                 CAL_FREE(query_str);
462                 return ret;
463         }
464         DBG("%s=%d", query_str, count);
465
466         *out_count = count;
467
468         if (bind_text) {
469                 g_slist_free_full(bind_text, free);
470                 bind_text = NULL;
471         }
472         CAL_FREE(query_str);
473         return CALENDAR_ERROR_NONE;
474 }
475