if db version is the latest, skip checking version logic
[platform/core/pim/calendar-service.git] / server / db / cal_db_plugin_instance_normal_extended.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.h"
29 #include "cal_db_query.h"
30 #include "cal_access_control.h"
31 #include "cal_utils.h"
32
33 static int _cal_db_instance_normal_extended_get_all_records(int offset, int limit, calendar_list_h* out_list);
34 static int _cal_db_instance_normal_extended_get_records_with_query(calendar_query_h query, int offset, int limit, calendar_list_h* out_list);
35 static int _cal_db_instance_normal_extended_get_count(int *out_count);
36 static int _cal_db_instance_normal_extended_get_count_with_query(calendar_query_h query, int *out_count);
37 /*
38  * static function
39  */
40 static void _cal_db_instance_normal_extended_get_stmt(sqlite3_stmt *stmt, calendar_record_h record);
41 static void _cal_db_instance_normal_extended_get_property_stmt(sqlite3_stmt *stmt,
42                 unsigned int property, int *stmt_count, calendar_record_h record);
43 static void _cal_db_instance_normal_extended_get_projection_stmt(sqlite3_stmt *stmt,
44                 const unsigned int *projection, const int projection_count,
45                 calendar_record_h record);
46
47 cal_db_plugin_cb_s cal_db_instance_normal_extended_plugin_cb = {
48         .is_query_only = false,
49         .insert_record = NULL,
50         .get_record = NULL,
51         .update_record = NULL,
52         .delete_record = NULL,
53         .get_all_records = _cal_db_instance_normal_extended_get_all_records,
54         .get_records_with_query = _cal_db_instance_normal_extended_get_records_with_query,
55         .insert_records = NULL,
56         .update_records = NULL,
57         .delete_records = NULL,
58         .get_count = _cal_db_instance_normal_extended_get_count,
59         .get_count_with_query = _cal_db_instance_normal_extended_get_count_with_query,
60         .replace_record = NULL,
61         .replace_records = NULL
62 };
63
64 static int _cal_db_instance_normal_extended_get_all_records(int offset, int limit, calendar_list_h* out_list)
65 {
66         int ret = CALENDAR_ERROR_NONE;
67         char offsetquery[CAL_DB_SQL_MAX_LEN] = {0};
68         char limitquery[CAL_DB_SQL_MAX_LEN] = {0};
69         sqlite3_stmt *stmt = NULL;
70
71         RETV_IF(NULL == out_list, CALENDAR_ERROR_INVALID_PARAMETER);
72
73         ret = calendar_list_create(out_list);
74         RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "calendar_list_create() Fail(%d)", ret);
75
76         if (0 < offset)
77                 snprintf(offsetquery, sizeof(offsetquery), "OFFSET %d", offset);
78         if (0 < limit)
79                 snprintf(limitquery, sizeof(limitquery), "LIMIT %d", limit);
80
81         char *query_str = NULL;
82         cal_db_append_string(&query_str, "SELECT * FROM");
83         cal_db_append_string(&query_str, CAL_VIEW_TABLE_NORMAL_INSTANCE_EXTENDED);
84         cal_db_append_string(&query_str, limitquery);
85         cal_db_append_string(&query_str, offsetquery);
86
87         ret = cal_db_util_query_prepare(query_str, &stmt);
88         if (CALENDAR_ERROR_NONE != ret) {
89                 /* LCOV_EXCL_START */
90                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
91                 SECURE("query[%s]", query_str);
92                 calendar_list_destroy(*out_list, true);
93                 *out_list = NULL;
94                 free(query_str);
95                 return ret;
96                 /* LCOV_EXCL_STOP */
97         }
98
99         while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
100                 calendar_record_h record;
101                 ret = calendar_record_create(_calendar_instance_utime_calendar_book_extended._uri, &record);
102                 if (CALENDAR_ERROR_NONE != ret) {
103                         /* LCOV_EXCL_START */
104                         calendar_list_destroy(*out_list, true);
105                         *out_list = NULL;
106                         sqlite3_finalize(stmt);
107                         CAL_FREE(query_str);
108                         return ret;
109                         /* LCOV_EXCL_STOP */
110                 }
111                 _cal_db_instance_normal_extended_get_stmt(stmt, record);
112
113                 ret = calendar_list_add(*out_list, record);
114                 if (CALENDAR_ERROR_NONE != ret) {
115                         /* LCOV_EXCL_START */
116                         calendar_list_destroy(*out_list, true);
117                         *out_list = NULL;
118                         calendar_record_destroy(record, true);
119                         sqlite3_finalize(stmt);
120                         CAL_FREE(query_str);
121                         return ret;
122                         /* LCOV_EXCL_STOP */
123                 }
124         }
125         sqlite3_finalize(stmt);
126         CAL_FREE(query_str);
127
128         return CALENDAR_ERROR_NONE;
129 }
130
131 static int _cal_db_instance_normal_extended_get_records_with_query(calendar_query_h query, int offset, int limit, calendar_list_h* out_list)
132 {
133         cal_query_s *que = NULL;
134         int ret = CALENDAR_ERROR_NONE;
135         char *condition = NULL;
136         char *projection = NULL;
137         GSList *bind_text = NULL, *cursor = NULL;
138         sqlite3_stmt *stmt = NULL;
139         int i = 0;
140         char *table_name;
141
142         que = (cal_query_s *)query;
143
144         table_name = cal_strdup(CAL_VIEW_TABLE_NORMAL_INSTANCE_EXTENDED);
145
146         /* make filter */
147         if (que->filter) {
148                 ret = cal_db_query_create_condition(query, &condition, &bind_text);
149                 if (CALENDAR_ERROR_NONE != ret) {
150                         /* LCOV_EXCL_START */
151                         ERR("cal_db_query_create_condition() Fail(%d), ret");
152                         CAL_FREE(table_name);
153                         return ret;
154                         /* LCOV_EXCL_STOP */
155                 }
156         }
157
158         /* make: projection */
159         ret = cal_db_query_create_projection(query, &projection);
160
161         char *query_str = NULL;
162         /* query: projection */
163         if (projection) {
164                 cal_db_append_string(&query_str, "SELECT");
165                 cal_db_append_string(&query_str, projection);
166                 cal_db_append_string(&query_str, "FROM");
167                 cal_db_append_string(&query_str, table_name);
168                 CAL_FREE(projection);
169         } else {
170                 cal_db_append_string(&query_str, "SELECT * FROM");
171                 cal_db_append_string(&query_str, table_name);
172         }
173         CAL_FREE(table_name);
174
175         /* query: condition */
176         if (condition) {
177                 cal_db_append_string(&query_str, "WHERE (");
178                 cal_db_append_string(&query_str, condition);
179                 cal_db_append_string(&query_str, ")");
180         }
181
182         /* order */
183         char *order = NULL;
184         ret = cal_db_query_create_order(query, condition, &order);
185         if (order) {
186                 cal_db_append_string(&query_str, order);
187                 CAL_FREE(order);
188         }
189         CAL_FREE(condition);
190
191         /* limit, offset */
192         char buf[CAL_STR_SHORT_LEN32] = {0};
193         if (0 < limit) {
194                 snprintf(buf, sizeof(buf), "LIMIT %d", limit);
195                 cal_db_append_string(&query_str, buf);
196
197                 if (0 < offset) {
198                         snprintf(buf, sizeof(buf), "OFFSET %d", offset);
199                         cal_db_append_string(&query_str, buf);
200                 }
201         }
202
203         /* query */
204         ret = cal_db_util_query_prepare(query_str, &stmt);
205         if (CALENDAR_ERROR_NONE != ret) {
206                 /* LCOV_EXCL_START */
207                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
208                 SECURE("query[%s]", query_str);
209                 if (bind_text) {
210                         g_slist_free_full(bind_text, free);
211                         bind_text = NULL;
212                 }
213                 free(query_str);
214                 return ret;
215                 /* LCOV_EXCL_STOP */
216         }
217
218         /* bind text */
219         if (bind_text) {
220                 g_slist_length(bind_text);
221                 for (cursor = bind_text, i = 1; cursor; cursor = cursor->next, i++)
222                         cal_db_util_stmt_bind_text(stmt, i, cursor->data);
223         }
224
225         ret = calendar_list_create(out_list);
226         if (CALENDAR_ERROR_NONE != ret) {
227                 /* LCOV_EXCL_START */
228                 ERR("calendar_list_create() Fail");
229                 if (bind_text) {
230                         g_slist_free_full(bind_text, free);
231                         bind_text = NULL;
232                 }
233                 sqlite3_finalize(stmt);
234                 CAL_FREE(query_str);
235                 return ret;
236                 /* LCOV_EXCL_STOP */
237         }
238
239         while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
240                 calendar_record_h record;
241                 ret = calendar_record_create(que->view_uri, &record);
242                 if (CALENDAR_ERROR_NONE != ret) {
243                         /* LCOV_EXCL_START */
244                         calendar_list_destroy(*out_list, true);
245                         *out_list = NULL;
246
247                         if (bind_text) {
248                                 g_slist_free_full(bind_text, free);
249                                 bind_text = NULL;
250                         }
251                         sqlite3_finalize(stmt);
252                         return ret;
253                         /* LCOV_EXCL_STOP */
254                 }
255
256                 if (0 < que->projection_count) {
257                         cal_record_set_projection(record,
258                                         que->projection, que->projection_count, que->property_count);
259
260                         _cal_db_instance_normal_extended_get_projection_stmt(stmt,
261                                         que->projection, que->projection_count, record);
262                 } else {
263                         _cal_db_instance_normal_extended_get_stmt(stmt, record);
264                 }
265
266                 ret = calendar_list_add(*out_list, record);
267                 if (CALENDAR_ERROR_NONE != ret) {
268                         /* LCOV_EXCL_START */
269                         calendar_list_destroy(*out_list, true);
270                         *out_list = NULL;
271                         calendar_record_destroy(record, true);
272
273                         if (bind_text) {
274                                 g_slist_free_full(bind_text, free);
275                                 bind_text = NULL;
276                         }
277                         sqlite3_finalize(stmt);
278                         CAL_FREE(query_str);
279                         return ret;
280                         /* LCOV_EXCL_STOP */
281                 }
282         }
283
284         if (bind_text) {
285                 g_slist_free_full(bind_text, free);
286                 bind_text = NULL;
287         }
288         sqlite3_finalize(stmt);
289         CAL_FREE(query_str);
290
291         return CALENDAR_ERROR_NONE;
292 }
293
294 static int _cal_db_instance_normal_extended_get_count(int *out_count)
295 {
296         RETV_IF(NULL == out_count, CALENDAR_ERROR_INVALID_PARAMETER);
297
298         char *query_str = NULL;
299         cal_db_append_string(&query_str, "SELECT count(*) FROM");
300         cal_db_append_string(&query_str, CAL_VIEW_TABLE_NORMAL_INSTANCE_EXTENDED);
301
302         int ret = 0;
303         int count = 0;
304         ret = cal_db_util_query_get_first_int_result(query_str, NULL, &count);
305         if (CALENDAR_ERROR_NONE != ret) {
306                 /* LCOV_EXCL_START */
307                 ERR("cal_db_util_query_get_first_int_result() Fail");
308                 CAL_FREE(query_str);
309                 return ret;
310                 /* LCOV_EXCL_STOP */
311         }
312         DBG("count(%d) str[%s]", count, query_str);
313         CAL_FREE(query_str);
314
315         *out_count = count;
316         return CALENDAR_ERROR_NONE;
317 }
318
319 static int _cal_db_instance_normal_extended_get_count_with_query(calendar_query_h query, int *out_count)
320 {
321         cal_query_s *que = NULL;
322         int ret = CALENDAR_ERROR_NONE;
323         char *condition = NULL;
324         char *table_name;
325         int count = 0;
326         GSList *bind_text = NULL;
327
328         que = (cal_query_s *)query;
329         table_name = cal_strdup(CAL_VIEW_TABLE_NORMAL_INSTANCE_EXTENDED);
330
331         /* make filter */
332         if (que->filter) {
333                 ret = cal_db_query_create_condition(query, &condition, &bind_text);
334                 if (CALENDAR_ERROR_NONE != ret) {
335                         /* LCOV_EXCL_START */
336                         ERR("cal_db_query_create_condition() Fail(%d), ret");
337                         CAL_FREE(table_name);
338                         return ret;
339                         /* LCOV_EXCL_STOP */
340                 }
341         }
342
343         char *query_str = NULL;
344         /* query: select */
345         cal_db_append_string(&query_str, "SELECT count(*) FROM");
346         cal_db_append_string(&query_str, table_name);
347         CAL_FREE(table_name);
348
349         /* query: condition */
350         if (condition) {
351                 cal_db_append_string(&query_str, "WHERE (");
352                 cal_db_append_string(&query_str, condition);
353                 cal_db_append_string(&query_str, ")");
354                 CAL_FREE(condition);
355         }
356
357         /* query */
358         ret = cal_db_util_query_get_first_int_result(query_str, bind_text, &count);
359         if (CALENDAR_ERROR_NONE != ret) {
360                 /* LCOV_EXCL_START */
361                 ERR("cal_db_util_query_get_first_int_result() Fail");
362                 if (bind_text) {
363                         g_slist_free_full(bind_text, free);
364                         bind_text = NULL;
365                 }
366
367                 CAL_FREE(query_str);
368                 return ret;
369                 /* LCOV_EXCL_STOP */
370         }
371         DBG("count(%d) str[%s]", count, query_str);
372
373         if (out_count) *out_count = count;
374         if (bind_text) {
375                 g_slist_free_full(bind_text, free);
376                 bind_text = NULL;
377         }
378
379         CAL_FREE(query_str);
380         return CALENDAR_ERROR_NONE;
381 }
382
383 static void _cal_db_instance_normal_extended_get_stmt(sqlite3_stmt *stmt, calendar_record_h record)
384 {
385         cal_instance_normal_extended_s* instance =  (cal_instance_normal_extended_s*)(record);
386         const unsigned char *temp;
387         int count = 0;
388
389         instance->event_id = sqlite3_column_int(stmt, count++);
390         instance->start.type = sqlite3_column_int(stmt, count++);
391         instance->start.time.utime = sqlite3_column_int64(stmt, count++);
392         count++; /* datatime */
393         instance->end.type = sqlite3_column_int(stmt, count++);
394         instance->end.time.utime = sqlite3_column_int64(stmt, count++);
395         count++; /* datatime */
396
397         temp = sqlite3_column_text(stmt, count++);
398         instance->summary = cal_strdup((const char*)temp);
399
400         temp = sqlite3_column_text(stmt, count++);
401         instance->description = cal_strdup((const char*)temp);
402
403         temp = sqlite3_column_text(stmt, count++);
404         instance->location = cal_strdup((const char*)temp);
405
406         instance->busy_status = sqlite3_column_int(stmt, count++);
407
408         instance->event_status = sqlite3_column_int(stmt, count++);
409
410         instance->priority = sqlite3_column_int(stmt, count++);
411
412         instance->sensitivity = sqlite3_column_int(stmt, count++);
413
414         instance->has_rrule = sqlite3_column_int(stmt, count++);
415         if (0 < instance->has_rrule)
416                 instance->has_rrule = 1;
417
418         instance->latitude = sqlite3_column_double(stmt, count++);
419         instance->longitude = sqlite3_column_double(stmt, count++);
420         instance->has_alarm = sqlite3_column_int(stmt, count++);
421         instance->original_event_id = sqlite3_column_int(stmt, count++);
422         instance->calendar_id = sqlite3_column_int(stmt, count++);
423         instance->last_mod = sqlite3_column_int64(stmt, count++);
424
425         temp = sqlite3_column_text(stmt, count++);
426         instance->sync_data1 = cal_strdup((const char*)temp);
427
428         temp = sqlite3_column_text(stmt, count++);
429         instance->organizer_name = cal_strdup((const char*)temp);
430
431         temp = sqlite3_column_text(stmt, count++);
432         instance->categories = cal_strdup((const char*)temp);
433
434         instance->has_attendee = sqlite3_column_int(stmt, count++);
435
436         temp = sqlite3_column_text(stmt, count++);
437         instance->sync_data2 = cal_strdup((const char*)temp);
438
439         temp = sqlite3_column_text(stmt, count++);
440         instance->sync_data3 = cal_strdup((const char*)temp);
441
442         temp = sqlite3_column_text(stmt, count++);
443         instance->sync_data4 = cal_strdup((const char*)temp);
444
445         return;
446 }
447
448 static void _cal_db_instance_normal_extended_get_property_stmt(sqlite3_stmt *stmt,
449                 unsigned int property, int *stmt_count, calendar_record_h record)
450 {
451         cal_instance_normal_extended_s* instance =  (cal_instance_normal_extended_s*)(record);
452         const unsigned char *temp;
453
454         switch (property) {
455         case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_START:
456                 instance->start.type = CALENDAR_TIME_UTIME;
457                 *stmt_count = *stmt_count+1;
458                 instance->start.time.utime = sqlite3_column_int64(stmt, *stmt_count);
459                 *stmt_count = *stmt_count+1;
460                 *stmt_count = *stmt_count+1;
461                 break;
462         case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_END:
463                 instance->end.type = CALENDAR_TIME_UTIME;
464                 *stmt_count = *stmt_count+1;
465                 instance->end.time.utime = sqlite3_column_int64(stmt, *stmt_count);
466                 *stmt_count = *stmt_count+1;
467                 *stmt_count = *stmt_count+1;
468                 break;
469         case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_SUMMARY:
470                 temp = sqlite3_column_text(stmt, *stmt_count);
471                 instance->summary = cal_strdup((const char*)temp);
472                 break;
473         case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_LOCATION:
474                 temp = sqlite3_column_text(stmt, *stmt_count);
475                 instance->location = cal_strdup((const char*)temp);
476                 break;
477         case  CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_CALENDAR_ID:
478                 instance->calendar_id = sqlite3_column_int(stmt, *stmt_count);
479                 break;
480         case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_DESCRIPTION:
481                 temp = sqlite3_column_text(stmt, *stmt_count);
482                 instance->description = cal_strdup((const char*)temp);
483                 break;
484         case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_BUSY_STATUS:
485                 instance->busy_status = sqlite3_column_int(stmt, *stmt_count);
486                 break;
487         case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_EVENT_STATUS:
488                 instance->event_status = sqlite3_column_int(stmt, *stmt_count);
489                 break;
490         case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_PRIORITY:
491                 instance->priority = sqlite3_column_int(stmt, *stmt_count);
492                 break;
493         case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_SENSITIVITY:
494                 instance->sensitivity = sqlite3_column_int(stmt, *stmt_count);
495                 break;
496         case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_HAS_RRULE:
497                 instance->has_rrule = sqlite3_column_int(stmt, *stmt_count);
498                 if (0 < instance->has_rrule)
499                         instance->has_rrule = 1;
500
501                 break;
502         case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_LATITUDE:
503                 instance->latitude = sqlite3_column_double(stmt, *stmt_count);
504                 break;
505         case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_LONGITUDE:
506                 instance->longitude = sqlite3_column_double(stmt, *stmt_count);
507                 break;
508         case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_EVENT_ID:
509                 instance->event_id = sqlite3_column_int(stmt, *stmt_count);
510                 break;
511         case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_HAS_ALARM:
512                 instance->has_alarm = sqlite3_column_int(stmt, *stmt_count);
513                 break;
514         case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_ORIGINAL_EVENT_ID:
515                 instance->original_event_id = sqlite3_column_int(stmt, *stmt_count);
516                 break;
517         case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_LAST_MODIFIED_TIME:
518                 instance->last_mod = sqlite3_column_int64(stmt, *stmt_count);
519                 break;
520         case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_SYNC_DATA1:
521                 temp = sqlite3_column_text(stmt, *stmt_count);
522                 instance->sync_data1 = cal_strdup((const char*)temp);
523                 break;
524         case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_ORGANIZER_NAME:
525                 temp = sqlite3_column_text(stmt, *stmt_count);
526                 instance->organizer_name = cal_strdup((const char*)temp);
527                 break;
528         case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_CATEGORIES:
529                 temp = sqlite3_column_text(stmt, *stmt_count);
530                 instance->categories = cal_strdup((const char*)temp);
531                 break;
532         case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_HAS_ATTENDEE:
533                 instance->has_attendee = sqlite3_column_int(stmt, *stmt_count);
534                 break;
535         case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_SYNC_DATA2:
536                 temp = sqlite3_column_text(stmt, *stmt_count);
537                 instance->sync_data2 = cal_strdup((const char*)temp);
538                 break;
539         case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_SYNC_DATA3:
540                 temp = sqlite3_column_text(stmt, *stmt_count);
541                 instance->sync_data3 = cal_strdup((const char*)temp);
542                 break;
543         case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_SYNC_DATA4:
544                 temp = sqlite3_column_text(stmt, *stmt_count);
545                 instance->sync_data4 = cal_strdup((const char*)temp);
546                 break;
547         default:
548                 sqlite3_column_int(stmt, *stmt_count);
549                 break;
550         }
551
552         *stmt_count = *stmt_count+1;
553
554         return;
555 }
556
557 static void _cal_db_instance_normal_extended_get_projection_stmt(sqlite3_stmt *stmt,
558                 const unsigned int *projection, const int projection_count,
559                 calendar_record_h record)
560 {
561         int i = 0;
562         int stmt_count = 0;
563
564         for (i = 0; i < projection_count; i++)
565                 _cal_db_instance_normal_extended_get_property_stmt(stmt, projection[i], &stmt_count, record);
566 }