modify terminology:calendar->book,allday->localtime,normal->utime,svc->service
[platform/core/pim/calendar-service.git] / server / db / cal_db_plugin_event.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 "calendar_db.h"
23 #include "cal_internal.h"
24 #include "cal_typedef.h"
25 #include "cal_view.h"
26 #include "cal_time.h"
27 #include "cal_record.h"
28 #include "cal_list.h"
29 #include "cal_db.h"
30 #include "cal_db_util.h"
31 #include "cal_db_rrule.h"
32 #include "cal_db_query.h"
33 #include "cal_db_plugin_alarm_helper.h"
34 #include "cal_db_instance.h"
35 #include "cal_db_plugin_attendee_helper.h"
36 #include "cal_db_plugin_extended_helper.h"
37 #include "cal_db_plugin_event_helper.h"
38 #include "cal_access_control.h"
39 #include "cal_db_plugin_timezone_helper.h"
40 #include "cal_utils.h"
41
42 static int _cal_db_event_insert_record(calendar_record_h record, int* id);
43 static int _cal_db_event_update_record(calendar_record_h record);
44 static int _cal_db_event_delete_record(int id);
45 static int _cal_db_event_replace_record(calendar_record_h record, int id);
46 static int _cal_db_event_delete_records(int ids[], int count);
47 static int _cal_db_event_get_record(int id, calendar_record_h* out_record);
48 static int _cal_db_event_get_all_records(int offset, int limit, calendar_list_h* out_list);
49 static int _cal_db_event_get_records_with_query(calendar_query_h query, int offset, int limit, calendar_list_h* out_list);
50 static int _cal_db_event_get_count(int *out_count);
51 static int _cal_db_event_get_count_with_query(calendar_query_h query, int *out_count);
52 static int _cal_db_event_update_dirty(calendar_record_h record, int is_dirty_in_time);
53 static int __update_record(calendar_record_h record, int is_dirty_in_time);
54
55 /*
56  * static function
57  */
58 static void _cal_db_event_get_stmt(sqlite3_stmt *stmt, bool is_view_table, calendar_record_h record, int *exception, int *extended);
59 static void _cal_db_event_get_property_stmt(sqlite3_stmt *stmt, unsigned int property, int *stmt_count, calendar_record_h record);
60 static void _cal_db_event_get_projection_stmt(sqlite3_stmt *stmt, const unsigned int *projection, const int projection_count, calendar_record_h record);
61 static int _cal_db_event_exception_get_records(int original_id, cal_list_s *list);
62 static int _cal_db_event_exception_delete_with_id(int original_id);
63 static int _cal_db_event_exception_get_ids(int original_id, GList **out_list);
64 static int _cal_db_event_exception_update(cal_list_s *exception_list_s, int original_id, int calendar_id, int is_dirty_in_time, time_t time_diff, int old_type, int new_type);
65 static int _cal_db_event_get_deleted_data(int id, int* calendar_book_id, int* created_ver, int* original_event_id, char** recurrence_id);
66 static int _cal_db_event_exdate_insert_utime(int event_id, const char* original_exdate, const char* exdate, int **exception_id, int *exception_len);
67 static bool _cal_db_event_check_calendar_book_type(calendar_record_h record);
68
69 static void __check_list(calendar_list_h l)
70 {
71         DBG("---------------------");
72         calendar_list_first(l);
73         int count = 0;
74         calendar_list_get_count(l, &count);
75         DBG("count(%d)", count);
76
77         int i;
78         for (i = 0; i < count; i++) {
79                 calendar_record_h event = NULL;
80                 calendar_list_get_current_record_p(l, &event);
81                 int id = 0;
82                 calendar_record_get_int(event, _calendar_event.id, &id);
83                 int book_id = 0;
84                 calendar_record_get_int(event, _calendar_event.calendar_book_id, &book_id);
85                 char *summary = NULL;
86                 calendar_record_get_str_p(event, _calendar_event.summary, &summary);
87                 int freq = 0;
88                 calendar_record_get_int(event, _calendar_event.freq, &freq);
89
90                 DBG("id(%d) book_id(%d) summary[%s] freq(%d)", id, book_id, summary, freq);
91                 calendar_list_next(l);
92         }
93 }
94
95 cal_db_plugin_cb_s cal_db_event_plugin_cb = {
96         .is_query_only = false,
97         .insert_record = _cal_db_event_insert_record,
98         .update_record = _cal_db_event_update_record,
99         .delete_record = _cal_db_event_delete_record,
100         .replace_record = _cal_db_event_replace_record,
101         .insert_records = NULL,
102         .update_records = NULL,
103         .delete_records = _cal_db_event_delete_records,
104         .replace_records = NULL,
105         .get_record = _cal_db_event_get_record,
106         .get_all_records = _cal_db_event_get_all_records,
107         .get_records_with_query = _cal_db_event_get_records_with_query,
108         .get_count = _cal_db_event_get_count,
109         .get_count_with_query = _cal_db_event_get_count_with_query
110 };
111
112 static int _cal_db_event_insert_record(calendar_record_h record, int* id)
113 {
114         RETV_IF(NULL == record, CALENDAR_ERROR_INVALID_PARAMETER);
115         RETV_IF(false == _cal_db_event_check_calendar_book_type(record), CALENDAR_ERROR_INVALID_PARAMETER);
116         cal_event_s *event = (cal_event_s *)record;
117         return cal_db_event_insert_record(record, event->original_event_id, id);
118 }
119
120 enum {
121         DIRTY_INIT = -1,
122         DIRTY_IN_OTHER = 0,
123         DIRTY_IN_TIME,
124 };
125
126 static int _cal_db_event_get_record(int id, calendar_record_h* out_record)
127 {
128         char query[CAL_DB_SQL_MAX_LEN];
129         int ret = 0;
130         sqlite3_stmt *stmt = NULL;
131         cal_event_s *event = NULL;
132         cal_rrule_s *rrule = NULL;
133         int exception = 0, extended = 0;
134         calendar_record_h record_calendar;
135         calendar_book_sync_event_type_e sync_event_type = CALENDAR_BOOK_SYNC_EVENT_FOR_ME;
136
137         ret = calendar_record_create(_calendar_event._uri, out_record);
138         if (CALENDAR_ERROR_NONE != ret) {
139                 /* LCOV_EXCL_START */
140                 ERR("calendar_record_create(%d)", ret);
141                 return CALENDAR_ERROR_OUT_OF_MEMORY;
142                 /* LCOV_EXCL_STOP */
143         }
144
145         event = (cal_event_s*)(*out_record);
146
147         snprintf(query, sizeof(query),
148                         "SELECT "CAL_QUERY_SCHEDULE_A_ALL" FROM %s AS A "
149                         "WHERE id = %d AND (type = %d OR type = %d) AND calendar_id IN "
150                         "(select id from %s where deleted = 0)",
151                         CAL_TABLE_SCHEDULE,
152                         id, CALENDAR_BOOK_TYPE_EVENT, CALENDAR_BOOK_TYPE_NONE,
153                         CAL_TABLE_CALENDAR);
154         ret = cal_db_util_query_prepare(query, &stmt);
155         if (CALENDAR_ERROR_NONE != ret) {
156                 /* LCOV_EXCL_START */
157                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
158                 SECURE("query[%s]", query);
159                 calendar_record_destroy(*out_record, true);
160                 *out_record = NULL;
161                 return ret;
162                 /* LCOV_EXCL_STOP */
163         }
164
165         ret = cal_db_util_stmt_step(stmt);
166         if (CAL_SQLITE_ROW != ret) {
167                 /* LCOV_EXCL_START */
168                 ERR("cal_db_util_stmt_step() Fail(%d)", ret);
169                 SECURE("[%s]", query);
170                 sqlite3_finalize(stmt);
171                 calendar_record_destroy(*out_record, true);
172                 *out_record = NULL;
173                 if (CALENDAR_ERROR_NONE == ret)
174                         return CALENDAR_ERROR_DB_RECORD_NOT_FOUND;
175                 return ret;
176                 /* LCOV_EXCL_STOP */
177         }
178
179         _cal_db_event_get_stmt(stmt, false, *out_record, &exception, &extended);
180         sqlite3_finalize(stmt);
181         stmt = NULL;
182
183         /* check */
184         ret = cal_db_get_record(_calendar_book._uri, event->calendar_id, &record_calendar);
185         if (CALENDAR_ERROR_NONE == ret) {
186                 ret = calendar_record_get_int(record_calendar,
187                                 _calendar_book.sync_event, (int *)&sync_event_type);
188                 calendar_record_destroy(record_calendar, true);
189         }
190         if (event->is_deleted == 1 && CALENDAR_BOOK_SYNC_EVENT_FOR_EVERY_AND_REMAIN != sync_event_type) {
191                 /* LCOV_EXCL_START */
192                 calendar_record_destroy(*out_record, true);
193                 *out_record = NULL;
194                 return CALENDAR_ERROR_DB_RECORD_NOT_FOUND;
195                 /* LCOV_EXCL_STOP */
196         }
197         if (CALENDAR_RECURRENCE_NONE != event->freq) {
198                 ret = cal_db_rrule_get_rrule(event->index, &rrule);
199                 if (CALENDAR_ERROR_NONE == ret) {
200                         cal_db_rrule_set_rrule_to_record(rrule, *out_record);
201                         CAL_FREE(rrule);
202                 }
203         }
204
205         if (event->has_alarm == 1)
206                 cal_db_alarm_get_records(event->index, event->alarm_list);
207
208         if (event->has_attendee == 1)
209                 cal_db_attendee_get_records(event->index, event->attendee_list);
210
211         if (exception == 1)
212                 _cal_db_event_exception_get_records(event->index, event->exception_list);
213
214         if (extended == 1)
215                 cal_db_extended_get_records(event->index, CALENDAR_RECORD_TYPE_EVENT, event->extended_list);
216
217         return CALENDAR_ERROR_NONE;
218 }
219
220 static int __is_dirty_in_time(calendar_record_h record)
221 {
222         cal_record_s *rec = (cal_record_s *)record;
223         RETV_IF(NULL == record, false);
224         RETV_IF(NULL == rec->view_uri, false);
225
226         int count = 0;
227         const cal_property_info_s *info = cal_view_get_property_info(rec->view_uri, &count);
228         RETVM_IF(NULL == info, false, "cal_view_get_property_info() Fail");
229
230         int i;
231         int is_dirty_in_time = DIRTY_IN_OTHER;
232         for (i = 0; i < count; i++) {
233                 if (false == cal_record_check_property_flag(record, info[i].property_id, CAL_PROPERTY_FLAG_DIRTY))
234                         continue;
235
236                 switch (info[i].property_id) {
237                 case CAL_PROPERTY_EVENT_START:
238                 case CAL_PROPERTY_EVENT_END:
239                 case CAL_PROPERTY_EVENT_START_TZID:
240                 case CAL_PROPERTY_EVENT_END_TZID:
241                 case CAL_PROPERTY_EVENT_FREQ:
242                 case CAL_PROPERTY_EVENT_RANGE_TYPE:
243                 case CAL_PROPERTY_EVENT_UNTIL:
244                 case CAL_PROPERTY_EVENT_COUNT:
245                 case CAL_PROPERTY_EVENT_INTERVAL:
246                 case CAL_PROPERTY_EVENT_BYSECOND:
247                 case CAL_PROPERTY_EVENT_BYMINUTE:
248                 case CAL_PROPERTY_EVENT_BYHOUR:
249                 case CAL_PROPERTY_EVENT_BYDAY:
250                 case CAL_PROPERTY_EVENT_BYMONTHDAY:
251                 case CAL_PROPERTY_EVENT_BYYEARDAY:
252                 case CAL_PROPERTY_EVENT_BYWEEKNO:
253                 case CAL_PROPERTY_EVENT_BYMONTH:
254                 case CAL_PROPERTY_EVENT_BYSETPOS:
255                 case CAL_PROPERTY_EVENT_WKST:
256
257                         is_dirty_in_time = DIRTY_IN_TIME;
258                         break;
259                 }
260                 if (DIRTY_IN_OTHER != is_dirty_in_time) break;
261         }
262         DBG("%sirty in time", DIRTY_IN_OTHER == is_dirty_in_time ? "Not d" : "D");
263         return is_dirty_in_time;
264 }
265
266 time_t __get_time_diff(char *old_tzid, calendar_time_s *old, char *new_tzid, calendar_time_s *new)
267 {
268         RETV_IF(NULL == old, CALENDAR_ERROR_INVALID_PARAMETER);
269         RETV_IF(NULL == new, CALENDAR_ERROR_INVALID_PARAMETER);
270
271         time_t diff = 0;
272         switch (old->type) {
273         case CALENDAR_TIME_UTIME:
274                 switch (new->type) {
275                 case CALENDAR_TIME_UTIME:
276                         DBG("%lld - %lld", old->time.utime, new->time.utime);
277                         diff = old->time.utime - new->time.utime;
278                         break;
279                 case CALENDAR_TIME_LOCALTIME:
280                         DBG("type is changed(%d) -> (%d)", old->type, new->type);
281                         diff = old->time.utime - cal_time_convert_itol(new_tzid,
282                                         new->time.date.year, new->time.date.month, new->time.date.mday,
283                                         new->time.date.hour, new->time.date.minute, new->time.date.second);
284                         break;
285                 }
286                 break;
287         case CALENDAR_TIME_LOCALTIME:
288                 switch (new->type) {
289                 case CALENDAR_TIME_UTIME:
290                         DBG("type is changed(%d) -> (%d)", old->type, new->type);
291                         diff = cal_time_convert_itol(old_tzid,
292                                         old->time.date.year, old->time.date.month, old->time.date.mday,
293                                         old->time.date.hour, old->time.date.minute, old->time.date.second) - new->time.utime;
294                         break;
295                 case CALENDAR_TIME_LOCALTIME:
296                         diff = cal_time_convert_itol(old_tzid,
297                                         old->time.date.year, old->time.date.month, old->time.date.mday,
298                                         old->time.date.hour, old->time.date.minute, old->time.date.second)
299                                 - cal_time_convert_itol(new_tzid, new->time.date.year, new->time.date.month, new->time.date.mday,
300                                                 new->time.date.hour, new->time.date.minute, new->time.date.second);
301                         break;
302                 }
303                 break;
304         }
305         DBG("-------------time diff(%ld)", diff);
306         return diff;
307 }
308
309 static int __get_time_shifted_field(char *old_field, int old_type, int new_type, time_t time_diff, char **new_field)
310 {
311         if (NULL == old_field || '\0' == *old_field)
312                 return CALENDAR_ERROR_NONE;
313
314         gchar **t = NULL;
315         t = g_strsplit_set(old_field, " ,", -1);
316         RETVM_IF(NULL == t, CALENDAR_ERROR_DB_FAILED, "g_strsplit_set() Fail");
317
318         int len_t = g_strv_length(t);
319
320         char *field = NULL;
321         int len_field = strlen(old_field);
322         len_field += (len_t * 8) + 1;  /* add (len_t * 8) for YYYYMMDD -> YYYYMMDDTHHMMSSZ */
323
324         field = calloc(len_field, sizeof(char)); /* add (len_t * 8) for YYYYMMDD -> YYYYMMDDTHHMMSSZ */
325         if (NULL == field) {
326                 /* LCOV_EXCL_START */
327                 ERR("calloc() Fail");
328                 g_strfreev(t);
329                 return CALENDAR_ERROR_OUT_OF_MEMORY;
330                 /* LCOV_EXCL_STOP */
331         }
332
333         struct tm tm = {0};
334         time_t tt = 0;
335
336         int len = 0;
337         int i;
338         for (i = 0; i < len_t; i++) {
339                 int y = 0, m = 0, d = 0;
340                 int h = 0, n = 0, s = 0;
341                 switch (old_type) {
342                 case CALENDAR_TIME_UTIME:
343                         sscanf(t[i], CAL_DATETIME_FORMAT_YYYYMMDDTHHMMSSZ, &y, &m, &d, &h, &n, &s);
344                         break;
345
346                 case CALENDAR_TIME_LOCALTIME:
347                         switch (strlen(t[i])) {
348                         case 8: /* YYYYMMDD */
349                                 sscanf(t[i], CAL_DATETIME_FORMAT_YYYYMMDD, &y, &m, &d);
350                                 break;
351
352                         case 15: /* YYYYMMDDTHHMMSS */
353                                 sscanf(t[i], CAL_DATETIME_FORMAT_YYYYMMDDTHHMMSS, &y, &m, &d, &h, &n, &s);
354                                 break;
355                         }
356                         break;
357                 }
358
359                 tm.tm_year = y - 1900;
360                 tm.tm_mon = m - 1;
361                 tm.tm_mday = d;
362                 tm.tm_hour = h;
363                 tm.tm_min = n;
364                 tm.tm_sec = s;
365
366                 char buf[CAL_STR_SHORT_LEN32] = {0};
367                 switch (new_type) {
368                 case CALENDAR_TIME_UTIME:
369                         switch (strlen(t[i])) {
370                         case 8: /* YYYYMMDD */
371                         case 15: /* YYYYMMDDTHHMMSS */
372                                 tt = timelocal(&tm) - time_diff;
373                                 gmtime_r(&tt, &tm);
374                                 snprintf(buf, sizeof(buf), "%s"CAL_DATETIME_FORMAT_YYYYMMDDTHHMMSSZ,
375                                                 i == 0 ? "" : ",", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
376                                                 tm.tm_hour, tm.tm_min, tm.tm_sec);
377                                 break;
378
379                         case 16: /* YYYYMMDDTHHMMSSZ */
380                                 tt = timegm(&tm) - time_diff;
381                                 gmtime_r(&tt, &tm);
382                                 snprintf(buf, sizeof(buf), "%s"CAL_DATETIME_FORMAT_YYYYMMDDTHHMMSSZ,
383                                                 i == 0 ? "" : ",", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
384                                                 tm.tm_hour, tm.tm_min, tm.tm_sec);
385                                 break;
386                         }
387
388                         break;
389
390                 case CALENDAR_TIME_LOCALTIME:
391                         switch (strlen(t[i])) {
392                         case 8: /* YYYYMMDD */
393                                 tt = timegm(&tm) - time_diff;
394                                 gmtime_r(&tt, &tm);
395                                 snprintf(buf, sizeof(buf), "%s"CAL_DATETIME_FORMAT_YYYYMMDD,
396                                                 i == 0 ? "" : ",", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
397                                 break;
398                         case 15: /* YYYYMMDDTHHMMSS */
399                                 tt = timegm(&tm) - time_diff;
400                                 gmtime_r(&tt, &tm);
401                                 snprintf(buf, sizeof(buf), "%s"CAL_DATETIME_FORMAT_YYYYMMDDTHHMMSS,
402                                                 i == 0 ? "" : ",", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
403                                                 tm.tm_hour, tm.tm_min, tm.tm_sec);
404                                 break;
405                         case 16: /* YYYYMMDDTHHMMSSZ */
406                                 tt = timegm(&tm) - time_diff;
407                                 localtime_r(&tt, &tm);
408                                 snprintf(buf, sizeof(buf), "%s"CAL_DATETIME_FORMAT_YYYYMMDDTHHMMSSZ,
409                                                 i == 0 ? "" : ",", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
410                                                 tm.tm_hour, tm.tm_min, tm.tm_sec);
411                                 break;
412                         }
413                         break;
414                 }
415                 len += snprintf(field + len, len_field -len, "%s", buf);
416                 DBG("[%s]", field);
417         }
418         g_strfreev(t);
419
420         if (new_field)
421                 *new_field = field;
422         else
423                 free(field);
424
425         return CALENDAR_ERROR_NONE;
426 }
427
428 static int __update_exdate(cal_event_s *rec, time_t time_diff)
429 {
430         RETV_IF(NULL == rec, CALENDAR_ERROR_INVALID_PARAMETER);
431
432         if (NULL == rec->exdate || '\0' == *rec->exdate)
433                 return CALENDAR_ERROR_NONE;
434         if (0 == time_diff)
435                 return CALENDAR_ERROR_NONE;
436
437         int ret;
438         calendar_record_h db_record = NULL;
439         ret = cal_db_get_record(_calendar_event._uri, rec->index, &db_record);
440         RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "calendar_db_get_record() Fail(%d)", ret);
441
442         char *db_tzid = NULL;
443         calendar_record_get_str_p(db_record, _calendar_event.start_tzid, &db_tzid);
444         calendar_time_s ct = {0};
445         calendar_record_get_caltime(db_record, _calendar_event.start_time, &ct);
446
447         char *new_exdate = NULL;
448         __get_time_shifted_field(rec->exdate, ct.type, rec->start.type, time_diff, &new_exdate);
449         free(rec->exdate);
450         rec->exdate = new_exdate;
451         DBG("new exdate[%s]", new_exdate);
452
453         calendar_record_destroy(db_record, true);
454
455         return CALENDAR_ERROR_NONE;
456 }
457
458 static int __update_recurrence_id(calendar_record_h exception, int old_type, int new_type, time_t time_diff)
459 {
460         CAL_FN_CALL();
461
462         cal_event_s *rec = (cal_event_s *)exception;
463         RETV_IF(NULL == rec, CALENDAR_ERROR_INVALID_PARAMETER);
464
465         if (NULL == rec->recurrence_id) {
466                 DBG("No recurrence_id");
467                 return CALENDAR_ERROR_NONE;
468         }
469
470         char *new_recurrence_id = NULL;
471         __get_time_shifted_field(rec->recurrence_id, rec->start.type, new_type, time_diff, &new_recurrence_id);
472         free(rec->recurrence_id);
473         rec->recurrence_id = new_recurrence_id;
474         DBG("new recurrence_id[%s]", new_recurrence_id);
475
476         return CALENDAR_ERROR_NONE;
477 }
478
479 static int __update_record(calendar_record_h record, int is_dirty_in_time)
480 {
481         int ret = 0;
482         char query[CAL_DB_SQL_MAX_LEN] = {0};
483         char dtstart_datetime[CAL_STR_SHORT_LEN32] = {0};
484         char dtend_datetime[CAL_STR_SHORT_LEN32] = {0};
485         sqlite3_stmt *stmt = NULL;
486         cal_event_s* event =  (cal_event_s*)(record);
487         cal_rrule_s *rrule = NULL;
488         int has_alarm = 0;
489         int timezone_id = 0;
490         int input_ver = 0;
491
492         RETV_IF(NULL == event, CALENDAR_ERROR_INVALID_PARAMETER);
493         RETV_IF(false == _cal_db_event_check_calendar_book_type(record), CALENDAR_ERROR_INVALID_PARAMETER);
494
495
496         ret = cal_db_event_check_value_validation(event);
497         if (CALENDAR_ERROR_NONE != ret) {
498                 /* LCOV_EXCL_START */
499                 ERR("cal_db_event_check_value_validation() Fail(%d)", ret);
500                 return ret;
501                 /* LCOV_EXCL_STOP */
502         }
503
504         /* access control */
505         if (cal_access_control_have_write_permission(event->calendar_id) == false) {
506                 /* LCOV_EXCL_START */
507                 ERR("cal_access_control_have_write_permission() Fail");
508                 return CALENDAR_ERROR_PERMISSION_DENIED;
509                 /* LCOV_EXCL_STOP */
510         }
511
512         if (event->common.properties_flags) {
513                 if (DIRTY_INIT == is_dirty_in_time)
514                         is_dirty_in_time = __is_dirty_in_time(record);
515
516                 return _cal_db_event_update_dirty(record, is_dirty_in_time);
517         }
518
519         int time_diff = 0;
520         calendar_time_s ct = {0};
521         if (DIRTY_IN_TIME == is_dirty_in_time) {
522                 calendar_record_h old_record = NULL;
523                 ret = cal_db_get_record(_calendar_event._uri, event->index, &old_record);
524                 if (CALENDAR_ERROR_NONE != ret) {
525                         /* LCOV_EXCL_START */
526                         ERR("calendar_db_get_record() Fail(%d)", ret);
527                         return ret;
528                         /* LCOV_EXCL_STOP */
529                 }
530
531                 /* get time diff */
532                 char *old_tzid = NULL;
533                 calendar_record_get_str_p(old_record, _calendar_event.start_tzid, &old_tzid);
534                 calendar_record_get_caltime(old_record, _calendar_event.start_time, &ct);
535                 time_diff = __get_time_diff(old_tzid, &ct, event->start_tzid, &event->start);
536                 calendar_record_destroy(old_record, true);
537         }
538
539         has_alarm = cal_db_alarm_has_alarm(event->alarm_list);
540         cal_db_timezone_search_with_tzid(event->calendar_id, event->start_tzid, &timezone_id);
541         input_ver = cal_db_util_get_next_ver();
542
543         int is_allday = 0;
544         if (CALENDAR_TIME_LOCALTIME == event->start.type
545                         && (0 == event->start.time.date.hour)
546                         && (0 == event->start.time.date.minute)
547                         && (0 == event->start.time.date.second)
548                         && (0 == event->end.time.date.hour)
549                         && (0 == event->end.time.date.minute)
550                         && (0 == event->end.time.date.second)) {
551                 is_allday = 1;
552         }
553         snprintf(query, sizeof(query), "UPDATE %s SET "
554                         "changed_ver = %d,"
555                         "type = %d,"
556                         "summary = ?,"
557                         "description = ?,"
558                         "location = ?,"
559                         "categories = ?,"
560                         "exdate = ?,"
561                         "task_status = %d,"
562                         "priority = %d,"
563                         "timezone = %d, "
564                         "contact_id = %d, "
565                         "busy_status = %d, "
566                         "sensitivity = %d, "
567                         "uid = ?, "
568                         "organizer_name = ?, "
569                         "organizer_email = ?, "
570                         "meeting_status = %d, "
571                         "calendar_id = %d, "
572                         "original_event_id = %d,"
573                         "latitude = %lf,"
574                         "longitude = %lf,"
575                         "email_id = %d,"
576                         "completed_time = %lld,"
577                         "progress = %d, "
578                         "dtstart_type = %d, "
579                         "dtstart_utime = %lld, "
580                         "dtstart_datetime = ?, "
581                         "dtstart_tzid = ?, "
582                         "dtend_type = %d, "
583                         "dtend_utime = %lld, "
584                         "dtend_datetime = ?, "
585                         "dtend_tzid = ?, "
586                         "last_mod = strftime('%%s', 'now'), "
587                         "rrule_id = %d, "
588                         "recurrence_id = ?, "
589                         "rdate = ?, "
590                         "has_attendee = %d, "
591                         "has_alarm = %d, "
592                         "system_type = %d, "
593                         "updated = %d, "
594                         "sync_data1 = ?, "
595                         "sync_data2 = ?, "
596                         "sync_data3 = ?, "
597                         "sync_data4 = ?,"
598                         "has_exception = %d, "
599                         "has_extended = %d, "
600                         "freq = %d, "
601                         "is_allday = %d "
602                         "WHERE id = %d;",
603                 CAL_TABLE_SCHEDULE,
604                 input_ver,
605                 CAL_SCH_TYPE_EVENT,/*event->cal_type,*/
606                 event->event_status,
607                 event->priority,
608                 event->timezone ? event->timezone : timezone_id,
609                 event->contact_id,
610                 event->busy_status,
611                 event->sensitivity,
612                 event->meeting_status,
613                 event->calendar_id,
614                 event->original_event_id,
615                 event->latitude,
616                 event->longitude,
617                 event->email_id,
618                 (long long int)0, /* event->completed_time */
619                 0, /* event->progress, */
620                 event->start.type,
621                 event->start.type == CALENDAR_TIME_UTIME ? event->start.time.utime : 0,
622                 event->end.type,
623                 event->end.type == CALENDAR_TIME_UTIME ? event->end.time.utime : 0,
624                 0 < event->freq ? 1 : 0,
625                 (event->attendee_list && 0 < event->attendee_list->count) ? 1 : 0,
626                 has_alarm,
627                 event->system_type,
628                 event->updated,
629                 (event->exception_list && 0 < event->exception_list->count) ? 1 : 0,
630                 (event->extended_list && 0 < event->extended_list->count) ? 1 : 0,
631                 event->freq,
632                 is_allday,
633                 event->index);
634
635         ret = cal_db_util_query_prepare(query, &stmt);
636         if (CALENDAR_ERROR_NONE != ret) {
637                 /* LCOV_EXCL_START */
638                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
639                 SECURE("query[%s]", query);
640                 return ret;
641                 /* LCOV_EXCL_STOP */
642         }
643
644         int index = 1;
645
646         if (event->summary)
647                 cal_db_util_stmt_bind_text(stmt, index, event->summary);
648         index++;
649
650         if (event->description)
651                 cal_db_util_stmt_bind_text(stmt, index, event->description);
652         index++;
653
654         if (event->location)
655                 cal_db_util_stmt_bind_text(stmt, index, event->location);
656         index++;
657
658         if (event->categories)
659                 cal_db_util_stmt_bind_text(stmt, index, event->categories);
660         index++;
661
662         if (event->exdate) {
663                 if (DIRTY_IN_TIME == is_dirty_in_time)
664                         __update_exdate(event, time_diff);
665
666                 cal_db_util_stmt_bind_text(stmt, index, event->exdate);
667         }
668         index++;
669
670         if (event->uid)
671                 cal_db_util_stmt_bind_text(stmt, index, event->uid);
672         index++;
673
674         if (event->organizer_name)
675                 cal_db_util_stmt_bind_text(stmt, index, event->organizer_name);
676         index++;
677
678         if (event->organizer_email)
679                 cal_db_util_stmt_bind_text(stmt, index, event->organizer_email);
680         index++;
681
682         if (CALENDAR_TIME_LOCALTIME == event->start.type) {
683                 snprintf(dtstart_datetime, sizeof(dtstart_datetime), CAL_FORMAT_LOCAL_DATETIME,
684                                 event->start.time.date.year,
685                                 event->start.time.date.month,
686                                 event->start.time.date.mday,
687                                 event->start.time.date.hour,
688                                 event->start.time.date.minute,
689                                 event->start.time.date.second);
690                 cal_db_util_stmt_bind_text(stmt, index, dtstart_datetime);
691         }
692         index++;
693
694         if (event->start_tzid)
695                 cal_db_util_stmt_bind_text(stmt, index, event->start_tzid);
696         index++;
697
698         if (CALENDAR_TIME_LOCALTIME == event->end.type) {
699                 snprintf(dtend_datetime, sizeof(dtend_datetime), CAL_FORMAT_LOCAL_DATETIME,
700                                 event->end.time.date.year,
701                                 event->end.time.date.month,
702                                 event->end.time.date.mday,
703                                 event->end.time.date.hour,
704                                 event->end.time.date.minute,
705                                 event->end.time.date.second);
706                 cal_db_util_stmt_bind_text(stmt, index, dtend_datetime);
707         }
708         index++;
709
710         if (event->end_tzid)
711                 cal_db_util_stmt_bind_text(stmt, index, event->end_tzid);
712         index++;
713
714         if (event->recurrence_id)
715                 cal_db_util_stmt_bind_text(stmt, index, event->recurrence_id);
716         index++;
717         if (event->rdate)
718                 cal_db_util_stmt_bind_text(stmt, index, event->rdate);
719         index++;
720         if (event->sync_data1)
721                 cal_db_util_stmt_bind_text(stmt, index, event->sync_data1);
722         index++;
723         if (event->sync_data2)
724                 cal_db_util_stmt_bind_text(stmt, index, event->sync_data2);
725         index++;
726         if (event->sync_data3)
727                 cal_db_util_stmt_bind_text(stmt, index, event->sync_data3);
728         index++;
729         if (event->sync_data4)
730                 cal_db_util_stmt_bind_text(stmt, index, event->sync_data4);
731         index++;
732
733         ret = cal_db_util_stmt_step(stmt);
734         sqlite3_finalize(stmt);
735         if (CALENDAR_ERROR_NONE != ret) {
736                 /* LCOV_EXCL_START */
737                 ERR("cal_db_util_stmt_step() Fail(%d)", ret);
738                 return ret;
739                 /* LCOV_EXCL_STOP */
740         }
741
742         /*
743          * update parent event changed ver in case this event is exception mod
744          * which is original_event_id > 0
745          */
746         cal_db_event_update_original_event_version(event->original_event_id, input_ver);
747         cal_db_rrule_get_rrule_from_record(record, &rrule);
748         cal_db_rrule_update_record(event->index, rrule); /* if rrule turns none, update 0. */
749         CAL_FREE(rrule);
750
751         if (DIRTY_IN_TIME == is_dirty_in_time) {
752                 cal_db_instance_discard_record(event->index);
753                 cal_db_instance_publish_record(record);
754
755         } else {
756                 cal_db_instance_update_exdate_del(event->index, event->exdate);
757         }
758
759         while ((event->uid && *event->uid) && (NULL == event->recurrence_id || '\0' == *event->recurrence_id)) {
760                 /* check if exception mod has. recurrence_id */
761                 GList *list = NULL;
762                 list = cal_db_event_get_list_with_uid(event->uid, event->index);
763                 if (NULL == list)
764                         break;
765                 GList *l = g_list_first(list);
766                 if (l) {
767                         int child_id = GPOINTER_TO_INT(l->data);
768                         /* update children original_event_id */
769                         cal_db_event_update_child_origina_event_id(child_id, event->index);
770                         char *recurrence_id = NULL;
771                         recurrence_id = cal_db_event_get_recurrence_id_from_exception(child_id);
772                         if (recurrence_id) {
773                                 /* remove parent instance */
774                                 cal_db_event_apply_recurrence_id(event->index, event, recurrence_id, child_id);
775                                 free(recurrence_id);
776                         }
777                         l = g_list_next(l);
778                 }
779                 g_list_free(list);
780                 break;
781         }
782
783         cal_db_alarm_delete_with_id(event->index);
784         cal_db_attendee_delete_with_id(event->index);
785         cal_db_extended_delete_with_id(event->index, CALENDAR_RECORD_TYPE_EVENT);
786
787         if (event->alarm_list && 0 < event->alarm_list->count) {
788                 ret = cal_db_alarm_insert_records(event->alarm_list, event->index);
789                 WARN_IF(CALENDAR_ERROR_NONE != ret, "cal_db_alarm_insert_records() Fail(%x)", ret);
790         }
791
792         if (event->attendee_list && 0 < event->attendee_list->count) {
793                 ret = cal_db_attendee_insert_records(event->attendee_list, event->index);
794                 WARN_IF(CALENDAR_ERROR_NONE != ret, "cal_db_attendee_insert_records() Fail(%x)", ret);
795         }
796
797         if (event->exception_list && 0 < event->exception_list->count) {
798                 ret = _cal_db_event_exception_update(event->exception_list, event->index, event->calendar_id, is_dirty_in_time, time_diff, ct.type, event->start.type);
799                 WARN_IF(CALENDAR_ERROR_NONE != ret, "_cal_db_event_exception_update() Fail(%d)", ret);
800         }
801
802         if (event->extended_list && 0 < event->extended_list->count) {
803                 ret = cal_db_extended_insert_records(event->extended_list, event->index, CALENDAR_RECORD_TYPE_EVENT);
804                 WARN_IF(CALENDAR_ERROR_NONE != ret, "cal_db_extended_insert_records() Fail(%d)", ret);
805         }
806
807         cal_db_util_notify(CAL_NOTI_TYPE_EVENT);
808
809         return CALENDAR_ERROR_NONE;
810 }
811
812 static int _cal_db_event_update_record(calendar_record_h record)
813 {
814         return __update_record(record, DIRTY_INIT);
815 }
816
817 static int _cal_db_event_add_exdate(int original_event_id, char* recurrence_id)
818 {
819         int ret = 0;
820         char query[CAL_DB_SQL_MAX_LEN];
821         sqlite3_stmt *stmt = NULL;
822
823         RETV_IF(original_event_id < 0, CALENDAR_ERROR_NONE);
824         DBG("This is exception mod event");
825         RETVM_IF(NULL == recurrence_id, CALENDAR_ERROR_NONE, "This event should have recurrence_id");
826         DBG("Exdate parent(id:%d) and recurrence_id[%s]", original_event_id, recurrence_id);
827
828         /* get exdate from original event */
829         snprintf(query, sizeof(query), "SELECT exdate FROM %s WHERE id = %d ",
830                         CAL_TABLE_SCHEDULE, original_event_id);
831
832         ret = cal_db_util_query_prepare(query, &stmt);
833         if (CALENDAR_ERROR_NONE != ret) {
834                 /* LCOV_EXCL_START */
835                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
836                 SECURE("query[%s]", query);
837                 return ret;
838                 /* LCOV_EXCL_STOP */
839         }
840
841         /* add recurrence id to end of the exdate of original event. */
842         const unsigned char *temp = NULL;
843         int len = 0;
844         char *exdate = NULL;
845         if (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
846                 temp = sqlite3_column_text(stmt, 0);
847                 if (NULL == temp || strlen((char *)temp) < 1) {
848                         exdate = cal_strdup(recurrence_id);
849                         DBG("append first exdate[%s]", exdate);
850                 } else {
851                         if (strstr((char *)temp, recurrence_id)) {
852                                 DBG("warn: recurrence id already is registered to exdate");
853                                 sqlite3_finalize(stmt);
854                                 return CALENDAR_ERROR_NONE;
855                         }
856                         len = strlen((const char *)temp) + strlen(recurrence_id) + 2;
857                         exdate = calloc(len, sizeof(char));
858                         if (NULL == exdate) {
859                                 /* LCOV_EXCL_START */
860                                 ERR("calloc() Fail");
861                                 sqlite3_finalize(stmt);
862                                 return CALENDAR_ERROR_OUT_OF_MEMORY;
863                                 /* LCOV_EXCL_STOP */
864                         }
865                         snprintf(exdate, len, "%s,%s", temp, recurrence_id);
866                         DBG("append [%s] to already has exdate [%s]", temp, recurrence_id);
867                 }
868         } else {
869                 DBG("Failed to get exdate: event_id(%d)", original_event_id);
870         }
871         sqlite3_finalize(stmt);
872         stmt = NULL;
873
874         /* update exdate */
875         DBG("update to recurrence id to exdate[%s]", exdate);
876         int input_ver = cal_db_util_get_next_ver();
877         snprintf(query, sizeof(query), "UPDATE %s SET exdate = ?, changed_ver=%d WHERE id = %d ",
878                         CAL_TABLE_SCHEDULE, input_ver, original_event_id);
879
880         ret = cal_db_util_query_prepare(query, &stmt);
881         if (CALENDAR_ERROR_NONE != ret) {
882                 /* LCOV_EXCL_START */
883                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
884                 SECURE("query[%s]", query);
885                 free(exdate);
886                 return ret;
887                 /* LCOV_EXCL_STOP */
888         }
889
890         int index = 1;
891         cal_db_util_stmt_bind_text(stmt, index, exdate);
892
893         ret = cal_db_util_stmt_step(stmt);
894         sqlite3_finalize(stmt);
895         free(exdate);
896         if (CALENDAR_ERROR_NONE != ret) {
897                 /* LCOV_EXCL_START */
898                 ERR("cal_db_util_stmt_step() Fail(%d)", ret);
899                 return ret;
900                 /* LCOV_EXCL_STOP */
901         }
902
903         return CALENDAR_ERROR_NONE;
904 }
905
906 static bool is_deleted_exception(int id)
907 {
908         int ret = CALENDAR_ERROR_NONE;
909         char query[CAL_DB_SQL_MAX_LEN] = {0};
910         snprintf(query, sizeof(query), "SELECT schedule_id FROM "CAL_TABLE_DELETED" "
911                         "WHERE schedule_id=%d", id);
912
913         sqlite3_stmt *stmt = NULL;
914         ret = cal_db_util_query_prepare(query, &stmt);
915         if (CALENDAR_ERROR_NONE != ret) {
916                 /* LCOV_EXCL_START */
917                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
918                 SECURE("query[%s]", query);
919                 return ret;
920                 /* LCOV_EXCL_STOP */
921         }
922
923         ret = cal_db_util_stmt_step(stmt);
924         sqlite3_finalize(stmt);
925
926         if (CAL_SQLITE_ROW == ret) {
927                 DBG("This record is already deleted, it could be exception record");
928                 return true;
929         }
930         return false;
931 }
932
933 static int _cal_db_event_delete_record(int id)
934 {
935         int ret = CALENDAR_ERROR_NONE;
936         int calendar_book_id;
937         char query[CAL_DB_SQL_MAX_LEN] = {0};
938         int created_ver = 0;
939         int original_event_id = 0;
940         char *recurrence_id = NULL;
941         calendar_book_sync_event_type_e sync_event_type = CALENDAR_BOOK_SYNC_EVENT_FOR_ME;
942
943         DBG("delete record(id:%d)", id);
944         RETVM_IF(id < 0, CALENDAR_ERROR_INVALID_PARAMETER, "id(%d) < 0", id);
945
946
947         /*
948          * if parent is deleted first, exception record is failed to be deleted.
949          * Becase exception record is deleted first, ret has SQLITE_DONE
950          * Trigger in schema deletes exception first, before deleting parent.
951          */
952         if (true == is_deleted_exception(id)) {
953                 return CALENDAR_ERROR_NONE;
954         }
955
956         /* get calendar_id, created_ver, original_event_id, recurrence_id */
957         ret = _cal_db_event_get_deleted_data(id, &calendar_book_id, &created_ver,
958                         &original_event_id, &recurrence_id);
959         if (CALENDAR_ERROR_NONE != ret) {
960                 /* LCOV_EXCL_START */
961                 DBG("_cal_db_event_get_deleted_data() Fail(%d)", ret);
962                 return ret;
963                 /* LCOV_EXCL_STOP */
964         }
965
966         /* access control */
967         if (cal_access_control_have_write_permission(calendar_book_id) == false) {
968                 /* LCOV_EXCL_START */
969                 ERR("Fail");
970                 CAL_FREE(recurrence_id);
971                 return CALENDAR_ERROR_PERMISSION_DENIED;
972                 /* LCOV_EXCL_STOP */
973         }
974
975         if (0 < original_event_id) {
976                 /* start:add record to exdate if this record is exception mod. */
977                 _cal_db_event_add_exdate(original_event_id, recurrence_id);
978         }
979         CAL_FREE(recurrence_id);
980
981         snprintf(query, sizeof(query), "SELECT sync_event FROM %s WHERE id = %d ", CAL_TABLE_CALENDAR, calendar_book_id);
982         ret = cal_db_util_query_get_first_int_result(query, NULL, (int *)&sync_event_type);
983         RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "cal_db_util_query_get_first_int_result() Fail(%d)", ret);
984
985         DBG("sync_event_type(%d)", sync_event_type);
986         if (sync_event_type == CALENDAR_BOOK_SYNC_EVENT_FOR_EVERY_AND_REMAIN) {
987                 DBG("set is_delete");
988                 snprintf(query, sizeof(query), "UPDATE %s SET is_deleted = 1, changed_ver = %d, "
989                                 "last_mod = strftime('%%s','now') WHERE id = %d ",
990                                 CAL_TABLE_SCHEDULE, cal_db_util_get_next_ver(), id);
991
992                 ret = cal_db_util_query_exec(query);
993                 if (CALENDAR_ERROR_NONE != ret) {
994                         /* LCOV_EXCL_START */
995                         ERR("cal_db_util_query_exec() Fail(%d)", ret);
996                         SECURE("[%s]", query);
997                         return ret;
998                         /* LCOV_EXCL_STOP */
999                 }
1000
1001         } else {
1002                 cal_db_util_get_next_ver();
1003
1004                 DBG("delete event");
1005                 snprintf(query, sizeof(query), "DELETE FROM %s WHERE id = %d ", CAL_TABLE_SCHEDULE, id);
1006                 ret = cal_db_util_query_exec(query);
1007                 if (CALENDAR_ERROR_NONE != ret) {
1008                         /* LCOV_EXCL_START */
1009                         ERR("cal_db_util_query_exec() Fail(%d)", ret);
1010                         SECURE("[%s]", query);
1011                         return ret;
1012                         /* LCOV_EXCL_STOP */
1013                 }
1014                 DBG("attendee, alarm and rrule is deleted by trigger");
1015         }
1016
1017         cal_db_instance_discard_record(id);
1018         cal_db_util_notify(CAL_NOTI_TYPE_EVENT);
1019         return CALENDAR_ERROR_NONE;
1020 }
1021
1022 static int _cal_db_event_get_all_records(int offset, int limit, calendar_list_h* out_list)
1023 {
1024         int ret = CALENDAR_ERROR_NONE;
1025         char offsetquery[CAL_DB_SQL_MAX_LEN] = {0};
1026         char limitquery[CAL_DB_SQL_MAX_LEN] = {0};
1027         sqlite3_stmt *stmt = NULL;
1028
1029         RETV_IF(NULL == out_list, CALENDAR_ERROR_INVALID_PARAMETER);
1030
1031         ret = calendar_list_create(out_list);
1032         RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "calendar_list_create() Fail(%d)", ret);
1033
1034         if (0 < offset)
1035                 snprintf(offsetquery, sizeof(offsetquery), "OFFSET %d", offset);
1036
1037         if (0 < limit)
1038                 snprintf(limitquery, sizeof(limitquery), "LIMIT %d", limit);
1039
1040         char *query_str = NULL;
1041         cal_db_append_string(&query_str, "SELECT * FROM");
1042         cal_db_append_string(&query_str, CAL_VIEW_TABLE_EVENT);
1043         cal_db_append_string(&query_str, limitquery);
1044         cal_db_append_string(&query_str, offsetquery);
1045
1046         ret = cal_db_util_query_prepare(query_str, &stmt);
1047         if (CALENDAR_ERROR_NONE != ret) {
1048                 /* LCOV_EXCL_START */
1049                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
1050                 SECURE("query[%s]", query_str);
1051                 calendar_list_destroy(*out_list, true);
1052                 *out_list = NULL;
1053                 free(query_str);
1054                 return ret;
1055                 /* LCOV_EXCL_STOP */
1056         }
1057
1058         SECURE("[TEST]---------query[%s]", query_str);
1059         while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
1060                 calendar_record_h record;
1061                 int exception = 0, extended = 0;
1062                 ret = calendar_record_create(_calendar_event._uri, &record);
1063                 if (CALENDAR_ERROR_NONE != ret) {
1064                         /* LCOV_EXCL_START */
1065                         ERR("calendar_record_create() Fail(%d)", ret);
1066                         calendar_list_destroy(*out_list, true);
1067                         *out_list = NULL;
1068                         sqlite3_finalize(stmt);
1069                         CAL_FREE(query_str);
1070                         return ret;
1071                         /* LCOV_EXCL_STOP */
1072                 }
1073                 _cal_db_event_get_stmt(stmt, true, record, &exception, &extended);
1074
1075                 /* child */
1076                 int has_attendee = 0, has_alarm = 0;
1077                 int record_id = 0;
1078                 cal_event_s* pevent = (cal_event_s*) record;
1079                 calendar_record_get_int(record, _calendar_event.id, &record_id);
1080                 if (CALENDAR_ERROR_NONE == calendar_record_get_int(record, _calendar_event.has_attendee, &has_attendee)) {
1081                         if (has_attendee == 1)
1082                                 cal_db_attendee_get_records(record_id, pevent->attendee_list);
1083                 }
1084                 if (CALENDAR_ERROR_NONE == calendar_record_get_int(record, _calendar_event.has_alarm, &has_alarm)) {
1085                         if (has_alarm == 1)
1086                                 cal_db_alarm_get_records(record_id, pevent->alarm_list);
1087                 }
1088
1089                 if (exception == 1)
1090                         _cal_db_event_exception_get_records(record_id, pevent->exception_list);
1091
1092                 if (extended == 1)
1093                         cal_db_extended_get_records(record_id, CALENDAR_RECORD_TYPE_EVENT, pevent->extended_list);
1094
1095                 ret = calendar_list_add(*out_list, record);
1096                 if (CALENDAR_ERROR_NONE != ret) {
1097                         /* LCOV_EXCL_START */
1098                         ERR("calendar_list_add() Fail(%d)", ret);
1099                         calendar_list_destroy(*out_list, true);
1100                         *out_list = NULL;
1101                         calendar_record_destroy(record, true);
1102                         sqlite3_finalize(stmt);
1103                         CAL_FREE(query_str);
1104                         return ret;
1105                         /* LCOV_EXCL_STOP */
1106                 }
1107         }
1108
1109         sqlite3_finalize(stmt);
1110         CAL_FREE(query_str);
1111         return CALENDAR_ERROR_NONE;
1112 }
1113
1114 static int _cal_db_event_get_records_with_query(calendar_query_h query, int offset, int limit, calendar_list_h* out_list)
1115 {
1116         cal_query_s *que = NULL;
1117         calendar_list_h list = NULL;
1118         int ret = CALENDAR_ERROR_NONE;
1119         char *condition = NULL;
1120         char *projection = NULL;
1121         GSList *bind_text = NULL, *cursor = NULL;
1122         sqlite3_stmt *stmt = NULL;
1123         int i = 0;
1124         char *table_name;
1125
1126         if (NULL == query || NULL == out_list) {
1127                 /* LCOV_EXCL_START */
1128                 ERR("Invalid parameter");
1129                 return CALENDAR_ERROR_INVALID_PARAMETER;
1130                 /* LCOV_EXCL_STOP */
1131         }
1132
1133         que = (cal_query_s *)query;
1134
1135         if (CAL_STRING_EQUAL == strcmp(que->view_uri, CALENDAR_VIEW_EVENT)) {
1136                 table_name = cal_strdup(CAL_VIEW_TABLE_EVENT);
1137         } else if (CAL_STRING_EQUAL == strcmp(que->view_uri, CALENDAR_VIEW_EVENT_BOOK)) {
1138                 table_name = cal_strdup(CAL_VIEW_TABLE_EVENT_BOOK);
1139         } else if (CAL_STRING_EQUAL == strcmp(que->view_uri, CALENDAR_VIEW_EVENT_BOOK_ATTENDEE)) {
1140                 table_name = cal_strdup(CAL_VIEW_TABLE_EVENT_BOOK_ATTENDEE);
1141         } else {
1142                 /* LCOV_EXCL_START */
1143                 ERR("uri(%s) not support get records with query", que->view_uri);
1144                 return CALENDAR_ERROR_INVALID_PARAMETER;
1145                 /* LCOV_EXCL_STOP */
1146         }
1147
1148         /* make filter */
1149         if (que->filter) {
1150                 ret = cal_db_query_create_condition(query, &condition, &bind_text);
1151                 if (CALENDAR_ERROR_NONE != ret) {
1152                         CAL_FREE(table_name);
1153                         /* LCOV_EXCL_START */
1154                         ERR("cal_db_query_create_condition() Fail(%d), ret");
1155                         return ret;
1156                         /* LCOV_EXCL_STOP */
1157                 }
1158         }
1159
1160         /* make: projection */
1161         ret = cal_db_query_create_projection(query, &projection);
1162
1163         char *query_str = NULL;
1164         /* query: projection */
1165         if (projection) {
1166                 cal_db_append_string(&query_str, "SELECT");
1167                 cal_db_append_string(&query_str, projection);
1168                 cal_db_append_string(&query_str, "FROM");
1169                 cal_db_append_string(&query_str, table_name);
1170                 CAL_FREE(projection);
1171         } else {
1172                 cal_db_append_string(&query_str, "SELECT * FROM");
1173                 cal_db_append_string(&query_str, table_name);
1174         }
1175         CAL_FREE(table_name);
1176
1177         /* query: condition */
1178         if (condition) {
1179                 cal_db_append_string(&query_str, "WHERE (");
1180                 cal_db_append_string(&query_str, condition);
1181                 cal_db_append_string(&query_str, ")");
1182         }
1183
1184         /* order */
1185         char *order = NULL;
1186         ret = cal_db_query_create_order(query, condition, &order);
1187         if (order) {
1188                 cal_db_append_string(&query_str, order);
1189                 CAL_FREE(order);
1190         }
1191         CAL_FREE(condition);
1192
1193         /* limit, offset */
1194         char buf[CAL_STR_SHORT_LEN32] = {0};
1195         if (0 < limit) {
1196                 snprintf(buf, sizeof(buf), "LIMIT %d", limit);
1197                 cal_db_append_string(&query_str, buf);
1198
1199                 if (0 < offset) {
1200                         snprintf(buf, sizeof(buf), "OFFSET %d", offset);
1201                         cal_db_append_string(&query_str, buf);
1202                 }
1203         }
1204         DBG("%s", query_str);
1205
1206         /* query */
1207         ret = cal_db_util_query_prepare(query_str, &stmt);
1208         if (CALENDAR_ERROR_NONE != ret) {
1209                 /* LCOV_EXCL_START */
1210                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
1211                 SECURE("query[%s]", query_str);
1212                 if (bind_text) {
1213                         g_slist_free_full(bind_text, free);
1214                         bind_text = NULL;
1215                 }
1216                 free(query_str);
1217                 return ret;
1218                 /* LCOV_EXCL_STOP */
1219         }
1220
1221         /* bind text */
1222         if (bind_text) {
1223                 for (cursor = bind_text, i = 1; cursor; cursor = cursor->next, i++)
1224                         cal_db_util_stmt_bind_text(stmt, i, cursor->data);
1225         }
1226
1227         ret = calendar_list_create(&list);
1228         if (CALENDAR_ERROR_NONE != ret) {
1229                 /* LCOV_EXCL_START */
1230                 ERR("calendar_list_create() Fail(%d)", ret);
1231                 if (bind_text) {
1232                         g_slist_free_full(bind_text, free);
1233                         bind_text = NULL;
1234                 }
1235                 sqlite3_finalize(stmt);
1236                 CAL_FREE(query_str);
1237                 return ret;
1238                 /* LCOV_EXCL_STOP */
1239         }
1240
1241         while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
1242                 calendar_record_h record;
1243                 int exception = 1, extended = 1;
1244                 int attendee = 1, alarm = 1;
1245
1246                 ret = calendar_record_create(_calendar_event._uri, &record);
1247                 if (CALENDAR_ERROR_NONE != ret) {
1248                         /* LCOV_EXCL_START */
1249                         ERR("calendar_record_create() Fail(%d)", ret);
1250                         calendar_list_destroy(list, true);
1251                         if (bind_text) {
1252                                 g_slist_free_full(bind_text, free);
1253                                 bind_text = NULL;
1254                         }
1255                         sqlite3_finalize(stmt);
1256                         CAL_FREE(query_str);
1257                         return ret;
1258                         /* LCOV_EXCL_STOP */
1259                 }
1260                 if (0 < que->projection_count) {
1261                         cal_record_set_projection(record, que->projection, que->projection_count, que->property_count);
1262                         _cal_db_event_get_projection_stmt(stmt, que->projection, que->projection_count, record);
1263                 } else {
1264                         cal_event_s *event = NULL;
1265                         _cal_db_event_get_stmt(stmt, true, record, &exception, &extended);
1266                         event = (cal_event_s*)(record);
1267                         if (event) {
1268                                 attendee = event->has_attendee;
1269                                 alarm = event->has_alarm;
1270                         }
1271                 }
1272
1273                 /* child */
1274                 if (cal_db_query_find_projection_property(query, CAL_PROPERTY_EVENT_CALENDAR_ALARM) == true && alarm == 1) {
1275                         cal_event_s* pevent = (cal_event_s*) record;
1276                         cal_db_alarm_get_records(pevent->index, pevent->alarm_list);
1277                 }
1278                 if (cal_db_query_find_projection_property(query, CAL_PROPERTY_EVENT_CALENDAR_ATTENDEE) == true && attendee == 1) {
1279                         cal_event_s* pevent = (cal_event_s*) record;
1280                         cal_db_attendee_get_records(pevent->index, pevent->attendee_list);
1281                 }
1282                 if (cal_db_query_find_projection_property(query, CAL_PROPERTY_EVENT_EXCEPTION) == true && exception == 1) {
1283                         cal_event_s* pevent = (cal_event_s*) record;
1284                         _cal_db_event_exception_get_records(pevent->index, pevent->exception_list);
1285                 }
1286                 if (cal_db_query_find_projection_property(query, CAL_PROPERTY_EVENT_EXTENDED) == true && extended == 1) {
1287                         cal_event_s* pevent = (cal_event_s*) record;
1288                         cal_db_extended_get_records(pevent->index, CALENDAR_RECORD_TYPE_EVENT, pevent->extended_list);
1289                 }
1290
1291                 ret = calendar_list_add(list, record);
1292                 if (CALENDAR_ERROR_NONE != ret) {
1293                         /* LCOV_EXCL_START */
1294                         ERR("calendar_list_add() Fail(%d)", ret);
1295                         calendar_list_destroy(list, true);
1296                         calendar_record_destroy(record, true);
1297
1298                         if (bind_text) {
1299                                 g_slist_free_full(bind_text, free);
1300                                 bind_text = NULL;
1301                         }
1302                         sqlite3_finalize(stmt);
1303                         CAL_FREE(query_str);
1304                         return ret;
1305                         /* LCOV_EXCL_STOP */
1306                 }
1307         }
1308
1309         if (bind_text) {
1310                 g_slist_free_full(bind_text, free);
1311                 bind_text = NULL;
1312         }
1313
1314         sqlite3_finalize(stmt);
1315         CAL_FREE(query_str);
1316
1317         *out_list = list;
1318         __check_list(list);
1319
1320         return CALENDAR_ERROR_NONE;
1321 }
1322
1323 static int _cal_db_event_delete_records(int ids[], int count)
1324 {
1325         int ret = CALENDAR_ERROR_NONE;
1326         int i = 0;
1327
1328         for (i = 0; i < count; i++) {
1329                 ret = _cal_db_event_delete_record(ids[i]);
1330                 if (CALENDAR_ERROR_NONE != ret) {
1331                         /* LCOV_EXCL_START */
1332                         ERR("_cal_db_event_delete_record() Fail(%d)", ret);
1333                         return ret;
1334                         /* LCOV_EXCL_STOP */
1335                 }
1336         }
1337         return CALENDAR_ERROR_NONE;
1338 }
1339
1340 static int _cal_db_event_get_count(int *out_count)
1341 {
1342         RETV_IF(NULL == out_count, CALENDAR_ERROR_INVALID_PARAMETER);
1343
1344         char *query_str = NULL;
1345         cal_db_append_string(&query_str, "SELECT count(*) FROM");
1346         cal_db_append_string(&query_str, CAL_VIEW_TABLE_EVENT);
1347
1348         int ret = 0;
1349         int count = 0;
1350         ret = cal_db_util_query_get_first_int_result(query_str, NULL, &count);
1351         if (CALENDAR_ERROR_NONE != ret) {
1352                 /* LCOV_EXCL_START */
1353                 ERR("cal_db_util_query_get_first_int_result() Fail");
1354                 CAL_FREE(query_str);
1355                 return ret;
1356                 /* LCOV_EXCL_STOP */
1357         }
1358         DBG("count(%d) str[%s]", count, query_str);
1359         CAL_FREE(query_str);
1360
1361         *out_count = count;
1362         return CALENDAR_ERROR_NONE;
1363 }
1364
1365 static int _cal_db_event_get_count_with_query(calendar_query_h query, int *out_count)
1366 {
1367         cal_query_s *que = NULL;
1368         int ret = CALENDAR_ERROR_NONE;
1369         char *condition = NULL;
1370         char *table_name;
1371         int count = 0;
1372         GSList *bind_text = NULL;
1373
1374         que = (cal_query_s *)query;
1375
1376         if (CAL_STRING_EQUAL == strcmp(que->view_uri, CALENDAR_VIEW_EVENT)) {
1377                 table_name = cal_strdup(CAL_VIEW_TABLE_EVENT);
1378         } else if (CAL_STRING_EQUAL == strcmp(que->view_uri, CALENDAR_VIEW_EVENT_BOOK)) {
1379                 table_name = cal_strdup(CAL_VIEW_TABLE_EVENT_BOOK);
1380         } else if (CAL_STRING_EQUAL == strcmp(que->view_uri, CALENDAR_VIEW_EVENT_BOOK_ATTENDEE)) {
1381                 table_name = cal_strdup(CAL_VIEW_TABLE_EVENT_BOOK_ATTENDEE);
1382         } else {
1383                 /* LCOV_EXCL_START */
1384                 ERR("uri(%s) not support get records with query", que->view_uri);
1385                 return CALENDAR_ERROR_INVALID_PARAMETER;
1386                 /* LCOV_EXCL_STOP */
1387         }
1388
1389         /* make filter */
1390         if (que->filter) {
1391                 ret = cal_db_query_create_condition(query, &condition, &bind_text);
1392                 if (CALENDAR_ERROR_NONE != ret) {
1393                         CAL_FREE(table_name);
1394                         /* LCOV_EXCL_START */
1395                         ERR("cal_db_query_create_condition() Fail(%d), ret");
1396                         return ret;
1397                         /* LCOV_EXCL_STOP */
1398                 }
1399         }
1400
1401         char *query_str = NULL;
1402         /* query: select */
1403         cal_db_append_string(&query_str, "SELECT count(*) FROM");
1404         cal_db_append_string(&query_str, table_name);
1405         CAL_FREE(table_name);
1406
1407         /* query: condition */
1408         if (condition) {
1409                 cal_db_append_string(&query_str,  "WHERE (");
1410                 cal_db_append_string(&query_str, condition);
1411                 cal_db_append_string(&query_str, ")");
1412                 CAL_FREE(condition);
1413         }
1414
1415         /* query */
1416         ret = cal_db_util_query_get_first_int_result(query_str, bind_text, &count);
1417         if (CALENDAR_ERROR_NONE != ret) {
1418                 /* LCOV_EXCL_START */
1419                 ERR("cal_db_util_query_get_first_int_result() Fail");
1420                 if (bind_text) {
1421                         g_slist_free_full(bind_text, free);
1422                         bind_text = NULL;
1423                 }
1424                 CAL_FREE(query_str);
1425                 return ret;
1426                 /* LCOV_EXCL_STOP */
1427         }
1428         DBG("count(%d) str[%s]", count, query_str);
1429
1430         if (out_count) *out_count = count;
1431         if (bind_text) {
1432                 g_slist_free_full(bind_text, free);
1433                 bind_text = NULL;
1434         }
1435
1436         CAL_FREE(query_str);
1437         return CALENDAR_ERROR_NONE;
1438 }
1439
1440 static int _cal_db_event_replace_record(calendar_record_h record, int id)
1441 {
1442         int ret = CALENDAR_ERROR_NONE;
1443         char query[CAL_DB_SQL_MAX_LEN] = {0};
1444         char dtstart_datetime[CAL_STR_SHORT_LEN32] = {0};
1445         char dtend_datetime[CAL_STR_SHORT_LEN32] = {0};
1446         sqlite3_stmt *stmt = NULL;
1447         cal_event_s* event =  (cal_event_s*)(record);
1448         cal_rrule_s *rrule = NULL;
1449         int has_alarm = 0;
1450         int timezone_id = 0;
1451         int input_ver = 0;
1452
1453         RETV_IF(NULL == event, CALENDAR_ERROR_INVALID_PARAMETER);
1454         event->index = id;
1455
1456         /* access control */
1457         if (cal_access_control_have_write_permission(event->calendar_id) == false) {
1458                 /* LCOV_EXCL_START */
1459                 ERR("cal_access_control_have_write_permission() Fail");
1460                 return CALENDAR_ERROR_PERMISSION_DENIED;
1461                 /* LCOV_EXCL_STOP */
1462         }
1463         if (event->common.properties_flags != NULL)
1464                 return _cal_db_event_update_dirty(record, -1);
1465
1466         has_alarm = cal_db_alarm_has_alarm(event->alarm_list);
1467         cal_db_timezone_search_with_tzid(event->calendar_id, event->start_tzid, &timezone_id);
1468         input_ver = cal_db_util_get_next_ver();
1469
1470         int is_allday = 0;
1471         if (CALENDAR_TIME_LOCALTIME == event->start.type
1472                         && (0 == event->start.time.date.hour)
1473                         && (0 == event->start.time.date.minute)
1474                         && (0 == event->start.time.date.second)
1475                         && (0 == event->end.time.date.hour)
1476                         && (0 == event->end.time.date.minute)
1477                         && (0 == event->end.time.date.second)) {
1478                 is_allday = 1;
1479         }
1480
1481         snprintf(query, sizeof(query), "UPDATE %s SET "
1482                         "changed_ver = %d,"
1483                         "type = %d,"
1484                         "summary = ?,"
1485                         "description = ?,"
1486                         "location = ?,"
1487                         "categories = ?,"
1488                         "exdate = ?,"
1489                         "task_status = %d,"
1490                         "priority = %d,"
1491                         "timezone = %d, "
1492                         "contact_id = %d, "
1493                         "busy_status = %d, "
1494                         "sensitivity = %d, "
1495                         "uid = ?, "
1496                         "organizer_name = ?, "
1497                         "organizer_email = ?, "
1498                         "meeting_status = %d, "
1499                         "calendar_id = %d, "
1500                         "original_event_id = %d,"
1501                         "latitude = %lf,"
1502                         "longitude = %lf,"
1503                         "email_id = %d,"
1504                         "completed_time = %lld,"
1505                         "progress = %d, "
1506                         "dtstart_type = %d, "
1507                         "dtstart_utime = %lld, "
1508                         "dtstart_datetime = ?, "
1509                         "dtstart_tzid = ?, "
1510                         "dtend_type = %d, "
1511                         "dtend_utime = %lld, "
1512                         "dtend_datetime = ?, "
1513                         "dtend_tzid = ?, "
1514                         "last_mod = strftime('%%s', 'now'), "
1515                         "rrule_id = %d, "
1516                         "recurrence_id = ?, "
1517                         "rdate = ?, "
1518                         "has_attendee = %d, "
1519                         "has_alarm = %d, "
1520                         "system_type = %d, "
1521                         "updated = %d, "
1522                         "sync_data1 = ?, "
1523                         "sync_data2 = ?, "
1524                         "sync_data3 = ?, "
1525                         "sync_data4 = ?, "
1526                         "freq = %d, "
1527                         "is_allday = %d "
1528                         "WHERE id = %d ",
1529                 CAL_TABLE_SCHEDULE,
1530                 input_ver,
1531                 CAL_SCH_TYPE_EVENT,/*event->cal_type,*/
1532                 event->event_status,
1533                 event->priority,
1534                 event->timezone ? event->timezone : timezone_id,
1535                 event->contact_id,
1536                 event->busy_status,
1537                 event->sensitivity,
1538                 event->meeting_status,
1539                 event->calendar_id,
1540                 event->original_event_id,
1541                 event->latitude,
1542                 event->longitude,
1543                 event->email_id,
1544                 (long long int)0, /* event->completed_time, */
1545                 0, /* event->progress, */
1546                 event->start.type,
1547                 event->start.type == CALENDAR_TIME_UTIME ? event->start.time.utime : 0,
1548                 event->end.type,
1549                 event->end.type == CALENDAR_TIME_UTIME ? event->end.time.utime : 0,
1550                 0 < event->freq ? 1 : 0,
1551                 (event->attendee_list && 0 < event->attendee_list->count) ? 1 : 0,
1552                 has_alarm,
1553                 event->system_type,
1554                 event->updated,
1555                 event->freq,
1556                 is_allday,
1557                 id);
1558
1559         ret = cal_db_util_query_prepare(query, &stmt);
1560         if (CALENDAR_ERROR_NONE != ret) {
1561                 /* LCOV_EXCL_START */
1562                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
1563                 SECURE("query[%s]", query);
1564                 return ret;
1565                 /* LCOV_EXCL_STOP */
1566         }
1567
1568         int index = 1;
1569
1570         if (event->summary)
1571                 cal_db_util_stmt_bind_text(stmt, index, event->summary);
1572         index++;
1573
1574         if (event->description)
1575                 cal_db_util_stmt_bind_text(stmt, index, event->description);
1576         index++;
1577
1578         if (event->location)
1579                 cal_db_util_stmt_bind_text(stmt, index, event->location);
1580         index++;
1581
1582         if (event->categories)
1583                 cal_db_util_stmt_bind_text(stmt, index, event->categories);
1584         index++;
1585
1586         if (event->exdate)
1587                 cal_db_util_stmt_bind_text(stmt, index, event->exdate);
1588         index++;
1589
1590         if (event->uid)
1591                 cal_db_util_stmt_bind_text(stmt, index, event->uid);
1592         index++;
1593
1594         if (event->organizer_name)
1595                 cal_db_util_stmt_bind_text(stmt, index, event->organizer_name);
1596         index++;
1597
1598         if (event->organizer_email)
1599                 cal_db_util_stmt_bind_text(stmt, index, event->organizer_email);
1600         index++;
1601
1602         if (CALENDAR_TIME_LOCALTIME == event->start.type) {
1603                 snprintf(dtstart_datetime, sizeof(dtstart_datetime), CAL_FORMAT_LOCAL_DATETIME,
1604                                 event->start.time.date.year,
1605                                 event->start.time.date.month,
1606                                 event->start.time.date.mday,
1607                                 event->start.time.date.hour,
1608                                 event->start.time.date.minute,
1609                                 event->start.time.date.second);
1610                 cal_db_util_stmt_bind_text(stmt, index, dtstart_datetime);
1611         }
1612         index++;
1613
1614         if (event->start_tzid)
1615                 cal_db_util_stmt_bind_text(stmt, index, event->start_tzid);
1616         index++;
1617
1618         if (CALENDAR_TIME_LOCALTIME == event->end.type) {
1619                 snprintf(dtend_datetime, sizeof(dtend_datetime), CAL_FORMAT_LOCAL_DATETIME,
1620                                 event->end.time.date.year,
1621                                 event->end.time.date.month,
1622                                 event->end.time.date.mday,
1623                                 event->end.time.date.hour,
1624                                 event->end.time.date.minute,
1625                                 event->end.time.date.second);
1626                 cal_db_util_stmt_bind_text(stmt, index, dtend_datetime);
1627         }
1628         index++;
1629
1630         if (event->end_tzid)
1631                 cal_db_util_stmt_bind_text(stmt, index, event->end_tzid);
1632         index++;
1633
1634         if (event->recurrence_id)
1635                 cal_db_util_stmt_bind_text(stmt, index, event->recurrence_id);
1636         index++;
1637         if (event->rdate)
1638                 cal_db_util_stmt_bind_text(stmt, index, event->rdate);
1639         index++;
1640         if (event->sync_data1)
1641                 cal_db_util_stmt_bind_text(stmt, index, event->sync_data1);
1642         index++;
1643         if (event->sync_data2)
1644                 cal_db_util_stmt_bind_text(stmt, index, event->sync_data2);
1645         index++;
1646         if (event->sync_data3)
1647                 cal_db_util_stmt_bind_text(stmt, index, event->sync_data3);
1648         index++;
1649         if (event->sync_data4)
1650                 cal_db_util_stmt_bind_text(stmt, index, event->sync_data4);
1651         index++;
1652
1653         ret = cal_db_util_stmt_step(stmt);
1654         sqlite3_finalize(stmt);
1655         if (CALENDAR_ERROR_NONE != ret) {
1656                 /* LCOV_EXCL_START */
1657                 ERR("cal_db_util_stmt_step() Fail(%d)", ret);
1658                 return ret;
1659                 /* LCOV_EXCL_STOP */
1660         }
1661
1662         /*
1663          * update parent event changed ver in case this event is exception mod
1664          * which is original_event_id > 0
1665          */
1666         cal_db_event_update_original_event_version(event->original_event_id, input_ver);
1667         cal_db_rrule_get_rrule_from_record(record, &rrule);
1668         cal_db_rrule_update_record(id, rrule);
1669         CAL_FREE(rrule);
1670
1671         cal_db_instance_discard_record(id);
1672         cal_db_instance_publish_record(record);
1673
1674         cal_db_alarm_delete_with_id(id);
1675         cal_db_attendee_delete_with_id(id);
1676         _cal_db_event_exception_delete_with_id(id);
1677         cal_db_extended_delete_with_id(id, CALENDAR_RECORD_TYPE_EVENT);
1678
1679         if (event->alarm_list && 0 < event->alarm_list->count) {
1680                 ret = cal_db_alarm_insert_records(event->alarm_list, event->index);
1681                 WARN_IF(CALENDAR_ERROR_NONE != ret, "cal_db_alarm_insert_records() Fail(%x)", ret);
1682         }
1683
1684         if (event->attendee_list && 0 < event->attendee_list->count) {
1685                 ret = cal_db_attendee_insert_records(event->attendee_list, event->index);
1686                 WARN_IF(CALENDAR_ERROR_NONE != ret, "cal_db_attendee_insert_records() Fail(%x)", ret);
1687         }
1688
1689         if (event->exception_list && 0 < event->exception_list->count) {
1690                 ret = cal_db_event_insert_records(event->exception_list, id);
1691                 WARN_IF(CALENDAR_ERROR_NONE != ret, "cal_db_event_insert_records() Fail(%x)", ret);
1692         }
1693
1694         if (event->extended_list && 0 < event->extended_list->count) {
1695                 ret = cal_db_extended_insert_records(event->extended_list, id, CALENDAR_RECORD_TYPE_EVENT);
1696                 WARN_IF(CALENDAR_ERROR_NONE != ret, "cal_db_extended_insert_records() Fail(%d)", ret);
1697         }
1698
1699         cal_db_util_notify(CAL_NOTI_TYPE_EVENT);
1700
1701         return CALENDAR_ERROR_NONE;
1702 }
1703
1704 static void _cal_db_event_get_stmt(sqlite3_stmt *stmt, bool is_view_table, calendar_record_h record,
1705                 int *exception, int *extended)
1706 {
1707         cal_event_s *event = NULL;
1708         const unsigned char *temp;
1709         int count = 0;
1710
1711         event = (cal_event_s*)(record);
1712
1713         event->index = sqlite3_column_int(stmt, count++);
1714         sqlite3_column_int(stmt, count++);
1715
1716         temp = sqlite3_column_text(stmt, count++);
1717         event->summary = cal_strdup((const char*)temp);
1718         temp = sqlite3_column_text(stmt, count++);
1719         event->description = cal_strdup((const char*)temp);
1720
1721         temp = sqlite3_column_text(stmt, count++);
1722         event->location = cal_strdup((const char*)temp);
1723
1724         temp = sqlite3_column_text(stmt, count++);
1725         event->categories = cal_strdup((const char*)temp);
1726
1727         temp = sqlite3_column_text(stmt, count++);
1728         event->exdate = cal_strdup((const char*)temp);
1729
1730         event->event_status = sqlite3_column_int(stmt, count++);
1731         event->priority = sqlite3_column_int(stmt, count++);
1732         event->timezone = sqlite3_column_int(stmt, count++);
1733         event->contact_id = sqlite3_column_int(stmt, count++);
1734         event->busy_status = sqlite3_column_int(stmt, count++);
1735         event->sensitivity = sqlite3_column_int(stmt, count++);
1736
1737         temp = sqlite3_column_text(stmt, count++);
1738         event->uid = cal_strdup((const char*)temp);
1739
1740         temp = sqlite3_column_text(stmt, count++);
1741         event->organizer_name = cal_strdup((const char*)temp);
1742
1743         temp = sqlite3_column_text(stmt, count++);
1744         event->organizer_email = cal_strdup((const char*)temp);
1745
1746         event->meeting_status = sqlite3_column_int(stmt, count++);
1747         event->calendar_id = sqlite3_column_int(stmt, count++);
1748         event->original_event_id = sqlite3_column_int(stmt, count++);
1749         event->latitude = sqlite3_column_double(stmt, count++);
1750         event->longitude = sqlite3_column_double(stmt, count++);
1751         event->email_id = sqlite3_column_int(stmt, count++);
1752         event->created_time = sqlite3_column_int64(stmt, count++);
1753
1754         count++; /* completed_time */
1755         count++; /* progress */
1756         count++; /* changed_ver */
1757         count++; /* created_ver */
1758
1759         event->is_deleted = sqlite3_column_int(stmt, count++);
1760         event->start.type = sqlite3_column_int(stmt, count++);
1761
1762         if (event->start.type == CALENDAR_TIME_UTIME) {
1763                 event->start.time.utime = sqlite3_column_int64(stmt, count++);
1764                 count++; /* dtstart_datetime */
1765         } else {
1766                 count++; /* dtstart_utime */
1767                 temp = sqlite3_column_text(stmt, count++);
1768                 if (temp) {
1769                         sscanf((const char *)temp, CAL_FORMAT_LOCAL_DATETIME, &(event->start.time.date.year),
1770                                         &(event->start.time.date.month), &(event->start.time.date.mday),
1771                                         &(event->start.time.date.hour), &(event->start.time.date.minute),
1772                                         &(event->start.time.date.second));
1773                 }
1774         }
1775
1776         temp = sqlite3_column_text(stmt, count++);
1777         event->start_tzid = cal_strdup((const char*)temp);
1778
1779         event->end.type = sqlite3_column_int(stmt, count++);
1780         if (event->end.type == CALENDAR_TIME_UTIME) {
1781                 event->end.time.utime = sqlite3_column_int64(stmt, count++);
1782                 count++; /* dtend_datetime */
1783         } else {
1784                 count++; /* dtend_utime */
1785                 temp = sqlite3_column_text(stmt, count++);
1786                 if (temp) {
1787                         sscanf((const char *)temp, CAL_FORMAT_LOCAL_DATETIME, &(event->end.time.date.year),
1788                                         &(event->end.time.date.month), &(event->end.time.date.mday),
1789                                         &(event->end.time.date.hour), &(event->end.time.date.minute),
1790                                         &(event->end.time.date.second));
1791                 }
1792         }
1793         temp = sqlite3_column_text(stmt, count++);
1794         event->end_tzid = cal_strdup((const char*)temp);
1795
1796         event->last_mod = sqlite3_column_int64(stmt, count++);
1797         sqlite3_column_int(stmt, count++);
1798
1799         temp = sqlite3_column_text(stmt, count++);
1800         event->recurrence_id = cal_strdup((const char*)temp);
1801         temp = sqlite3_column_text(stmt, count++);
1802         event->rdate = cal_strdup((const char*)temp);
1803         event->has_attendee = sqlite3_column_int(stmt, count++);
1804         event->has_alarm = sqlite3_column_int(stmt, count++);
1805         event->system_type = sqlite3_column_int(stmt, count++);
1806         event->updated = sqlite3_column_int(stmt, count++);
1807         temp = sqlite3_column_text(stmt, count++);
1808         event->sync_data1 = cal_strdup((const char*)temp);
1809         temp = sqlite3_column_text(stmt, count++);
1810         event->sync_data2 = cal_strdup((const char*)temp);
1811         temp = sqlite3_column_text(stmt, count++);
1812         event->sync_data3 = cal_strdup((const char*)temp);
1813         temp = sqlite3_column_text(stmt, count++);
1814         event->sync_data4 = cal_strdup((const char*)temp);
1815
1816         /* has_exception */
1817         if (exception)
1818                 *exception = sqlite3_column_int(stmt, count++);
1819
1820         /* has_extended */
1821         if (extended)
1822                 *extended = sqlite3_column_int(stmt, count++);
1823
1824         event->freq = sqlite3_column_int(stmt, count++);
1825         event->is_allday = sqlite3_column_int(stmt, count++);
1826
1827         if (is_view_table == true) {
1828                 if (event->freq <= 0)
1829                         return ;
1830
1831                 event->range_type = sqlite3_column_int(stmt, count++);
1832                 event->until.type = sqlite3_column_int(stmt, count++);
1833
1834                 switch (event->until.type) {
1835                 case CALENDAR_TIME_UTIME:
1836                         event->until.time.utime = sqlite3_column_int64(stmt, count++);
1837                         count++; /* datetime */
1838                         break;
1839
1840                 case CALENDAR_TIME_LOCALTIME:
1841                         count++; /* utime */
1842                         temp = sqlite3_column_text(stmt, count++);
1843                         if (temp) {
1844                                 sscanf((const char *)temp, CAL_FORMAT_LOCAL_DATETIME, &(event->until.time.date.year),
1845                                                 &(event->until.time.date.month), &(event->until.time.date.mday),
1846                                                 &(event->until.time.date.hour), &(event->until.time.date.minute),
1847                                                 &(event->until.time.date.second));
1848                         }
1849                         break;
1850                 }
1851
1852                 event->count = sqlite3_column_int(stmt, count++);
1853                 event->interval = sqlite3_column_int(stmt, count++);
1854
1855                 temp = sqlite3_column_text(stmt, count++);
1856                 event->bysecond = cal_strdup((const char*)temp);
1857
1858                 temp = sqlite3_column_text(stmt, count++);
1859                 event->byminute = cal_strdup((const char*)temp);
1860
1861                 temp = sqlite3_column_text(stmt, count++);
1862                 event->byhour = cal_strdup((const char*)temp);
1863
1864                 temp = sqlite3_column_text(stmt, count++);
1865                 event->byday = cal_strdup((const char*)temp);
1866
1867                 temp = sqlite3_column_text(stmt, count++);
1868                 event->bymonthday = cal_strdup((const char*)temp);
1869
1870                 temp = sqlite3_column_text(stmt, count++);
1871                 event->byyearday = cal_strdup((const char*)temp);
1872
1873                 temp = sqlite3_column_text(stmt, count++);
1874                 event->byweekno = cal_strdup((const char*)temp);
1875
1876                 temp = sqlite3_column_text(stmt, count++);
1877                 event->bymonth = cal_strdup((const char*)temp);
1878
1879                 temp = sqlite3_column_text(stmt, count++);
1880                 event->bysetpos = cal_strdup((const char*)temp);
1881
1882                 event->wkst = sqlite3_column_int(stmt, count++);
1883
1884                 sqlite3_column_int(stmt, count++); /* calendar deleted */
1885         }
1886
1887 }
1888
1889 static void _cal_db_event_get_property_stmt(sqlite3_stmt *stmt,
1890                 unsigned int property, int *stmt_count, calendar_record_h record)
1891 {
1892         cal_event_s *event = NULL;
1893         const unsigned char *temp;
1894
1895         event = (cal_event_s*)(record);
1896
1897         switch (property) {
1898         case CAL_PROPERTY_EVENT_ID:
1899                 event->index = sqlite3_column_int(stmt, *stmt_count);
1900                 break;
1901         case CAL_PROPERTY_EVENT_BOOK_ID:
1902                 event->calendar_id = sqlite3_column_int(stmt, *stmt_count);
1903                 break;
1904         case CAL_PROPERTY_EVENT_SUMMARY:
1905                 temp = sqlite3_column_text(stmt, *stmt_count);
1906                 event->summary = cal_strdup((const char*)temp);
1907                 break;
1908         case CAL_PROPERTY_EVENT_DESCRIPTION:
1909                 temp = sqlite3_column_text(stmt, *stmt_count);
1910                 event->description = cal_strdup((const char*)temp);
1911                 break;
1912         case CAL_PROPERTY_EVENT_LOCATION:
1913                 temp = sqlite3_column_text(stmt, *stmt_count);
1914                 event->location = cal_strdup((const char*)temp);
1915                 break;
1916         case CAL_PROPERTY_EVENT_CATEGORIES:
1917                 temp = sqlite3_column_text(stmt, *stmt_count);
1918                 event->categories = cal_strdup((const char*)temp);
1919                 break;
1920         case CAL_PROPERTY_EVENT_EXDATE:
1921                 temp = sqlite3_column_text(stmt, *stmt_count);
1922                 event->exdate = cal_strdup((const char*)temp);
1923                 break;
1924         case CAL_PROPERTY_EVENT_EVENT_STATUS:
1925                 event->event_status = sqlite3_column_int(stmt, *stmt_count);
1926                 break;
1927         case CAL_PROPERTY_EVENT_PRIORITY:
1928                 event->priority = sqlite3_column_int(stmt, *stmt_count);
1929                 break;
1930         case CAL_PROPERTY_EVENT_TIMEZONE:
1931                 event->timezone = sqlite3_column_int(stmt, *stmt_count);
1932                 break;
1933         case CAL_PROPERTY_EVENT_CONTACT_ID:
1934                 event->contact_id = sqlite3_column_int(stmt, *stmt_count);
1935                 break;
1936         case CAL_PROPERTY_EVENT_BUSY_STATUS:
1937                 event->busy_status = sqlite3_column_int(stmt, *stmt_count);
1938                 break;
1939         case CAL_PROPERTY_EVENT_SENSITIVITY:
1940                 event->sensitivity = sqlite3_column_int(stmt, *stmt_count);
1941                 break;
1942         case CAL_PROPERTY_EVENT_UID:
1943                 temp = sqlite3_column_text(stmt, *stmt_count);
1944                 event->uid = cal_strdup((const char*)temp);
1945                 break;
1946         case CAL_PROPERTY_EVENT_ORGANIZER_NAME:
1947                 temp = sqlite3_column_text(stmt, *stmt_count);
1948                 event->organizer_name = cal_strdup((const char*)temp);
1949                 break;
1950         case CAL_PROPERTY_EVENT_ORGANIZER_EMAIL:
1951                 temp = sqlite3_column_text(stmt, *stmt_count);
1952                 event->organizer_email = cal_strdup((const char*)temp);
1953                 break;
1954         case CAL_PROPERTY_EVENT_MEETING_STATUS:
1955                 event->meeting_status = sqlite3_column_int(stmt, *stmt_count);
1956                 break;
1957         case CAL_PROPERTY_EVENT_ORIGINAL_EVENT_ID:
1958                 event->original_event_id = sqlite3_column_int(stmt, *stmt_count);
1959                 break;
1960         case CAL_PROPERTY_EVENT_LATITUDE:
1961                 event->latitude = sqlite3_column_double(stmt, *stmt_count);
1962                 break;
1963         case CAL_PROPERTY_EVENT_LONGITUDE:
1964                 event->longitude = sqlite3_column_double(stmt, *stmt_count);
1965                 break;
1966         case CAL_PROPERTY_EVENT_EMAIL_ID:
1967                 event->email_id = sqlite3_column_int(stmt, *stmt_count);
1968                 break;
1969         case CAL_PROPERTY_EVENT_CREATED_TIME:
1970                 event->created_time = sqlite3_column_int64(stmt, *stmt_count);
1971                 break;
1972         case CAL_PROPERTY_EVENT_LAST_MODIFIED_TIME:
1973                 event->last_mod = sqlite3_column_int64(stmt, *stmt_count);
1974                 break;
1975         case CAL_PROPERTY_EVENT_IS_DELETED:
1976                 event->is_deleted = sqlite3_column_int(stmt, *stmt_count);
1977                 break;
1978         case CAL_PROPERTY_EVENT_FREQ:
1979                 event->freq = sqlite3_column_int(stmt, *stmt_count);
1980                 break;
1981         case CAL_PROPERTY_EVENT_RANGE_TYPE:
1982                 event->range_type = sqlite3_column_int(stmt, *stmt_count);
1983                 break;
1984         case CAL_PROPERTY_EVENT_UNTIL:
1985                 event->until.type = sqlite3_column_int(stmt, *stmt_count);
1986                 if (event->until.type == CALENDAR_TIME_UTIME) {
1987                         *stmt_count = *stmt_count+1;
1988                         event->until.time.utime = sqlite3_column_int64(stmt, *stmt_count);
1989                         *stmt_count = *stmt_count+1; /* until_datetime */
1990                 } else {
1991                         *stmt_count = *stmt_count+1;
1992                         *stmt_count = *stmt_count+1;
1993                         temp = sqlite3_column_text(stmt, *stmt_count);
1994                         if (temp) {
1995                                 sscanf((const char *)temp, CAL_FORMAT_LOCAL_DATETIME, &(event->until.time.date.year),
1996                                                 &(event->until.time.date.month), &(event->until.time.date.mday),
1997                                                 &(event->until.time.date.hour), &(event->until.time.date.minute),
1998                                                 &(event->until.time.date.second));
1999                         }
2000                 }
2001                 break;
2002         case CAL_PROPERTY_EVENT_COUNT:
2003                 event->count = sqlite3_column_int(stmt, *stmt_count);
2004                 break;
2005         case CAL_PROPERTY_EVENT_INTERVAL:
2006                 event->interval = sqlite3_column_int(stmt, *stmt_count);
2007                 break;
2008         case CAL_PROPERTY_EVENT_BYSECOND:
2009                 temp = sqlite3_column_text(stmt, *stmt_count);
2010                 event->bysecond = cal_strdup((const char*)temp);
2011                 break;
2012         case CAL_PROPERTY_EVENT_BYMINUTE:
2013                 temp = sqlite3_column_text(stmt, *stmt_count);
2014                 event->byminute = cal_strdup((const char*)temp);
2015                 break;
2016         case CAL_PROPERTY_EVENT_BYHOUR:
2017                 temp = sqlite3_column_text(stmt, *stmt_count);
2018                 event->byhour = cal_strdup((const char*)temp);
2019                 break;
2020         case CAL_PROPERTY_EVENT_BYDAY:
2021                 temp = sqlite3_column_text(stmt, *stmt_count);
2022                 event->byday = cal_strdup((const char*)temp);
2023                 break;
2024         case CAL_PROPERTY_EVENT_BYMONTHDAY:
2025                 temp = sqlite3_column_text(stmt, *stmt_count);
2026                 event->bymonthday = cal_strdup((const char*)temp);
2027                 break;
2028         case CAL_PROPERTY_EVENT_BYYEARDAY:
2029                 temp = sqlite3_column_text(stmt, *stmt_count);
2030                 event->byyearday = cal_strdup((const char*)temp);
2031                 break;
2032         case CAL_PROPERTY_EVENT_BYWEEKNO:
2033                 temp = sqlite3_column_text(stmt, *stmt_count);
2034                 event->byweekno = cal_strdup((const char*)temp);
2035                 break;
2036         case CAL_PROPERTY_EVENT_BYMONTH:
2037                 temp = sqlite3_column_text(stmt, *stmt_count);
2038                 event->bymonth = cal_strdup((const char*)temp);
2039                 break;
2040         case CAL_PROPERTY_EVENT_BYSETPOS:
2041                 temp = sqlite3_column_text(stmt, *stmt_count);
2042                 event->bysetpos = cal_strdup((const char*)temp);
2043                 break;
2044         case CAL_PROPERTY_EVENT_WKST:
2045                 event->wkst = sqlite3_column_int(stmt, *stmt_count);
2046                 break;
2047         case CAL_PROPERTY_EVENT_RECURRENCE_ID:
2048                 temp = sqlite3_column_text(stmt, *stmt_count);
2049                 event->recurrence_id = cal_strdup((const char*)temp);
2050                 break;
2051         case CAL_PROPERTY_EVENT_RDATE:
2052                 temp = sqlite3_column_text(stmt, *stmt_count);
2053                 event->rdate = cal_strdup((const char*)temp);
2054                 break;
2055         case CAL_PROPERTY_EVENT_HAS_ATTENDEE:
2056                 event->has_attendee = sqlite3_column_int(stmt, *stmt_count);
2057                 break;
2058         case CAL_PROPERTY_EVENT_HAS_ALARM:
2059                 event->has_alarm = sqlite3_column_int(stmt, *stmt_count);
2060                 break;
2061         case CAL_PROPERTY_EVENT_SYNC_DATA1:
2062                 temp = sqlite3_column_text(stmt, *stmt_count);
2063                 event->sync_data1 = cal_strdup((const char*)temp);
2064                 break;
2065         case CAL_PROPERTY_EVENT_SYNC_DATA2:
2066                 temp = sqlite3_column_text(stmt, *stmt_count);
2067                 event->sync_data2 = cal_strdup((const char*)temp);
2068                 break;
2069         case CAL_PROPERTY_EVENT_SYNC_DATA3:
2070                 temp = sqlite3_column_text(stmt, *stmt_count);
2071                 event->sync_data3 = cal_strdup((const char*)temp);
2072                 break;
2073         case CAL_PROPERTY_EVENT_SYNC_DATA4:
2074                 temp = sqlite3_column_text(stmt, *stmt_count);
2075                 event->sync_data4 = cal_strdup((const char*)temp);
2076                 break;
2077         case CAL_PROPERTY_EVENT_START:
2078                 event->start.type = sqlite3_column_int(stmt, *stmt_count);
2079                 if (event->start.type == CALENDAR_TIME_UTIME) {
2080                         *stmt_count = *stmt_count+1;
2081                         event->start.time.utime = sqlite3_column_int64(stmt, *stmt_count);
2082                         *stmt_count = *stmt_count+1; /* dtstart_datetime */
2083                 } else {
2084                         *stmt_count = *stmt_count+1;
2085                         *stmt_count = *stmt_count+1;
2086                         temp = sqlite3_column_text(stmt, *stmt_count);
2087                         if (temp) {
2088                                 sscanf((const char *)temp, CAL_FORMAT_LOCAL_DATETIME, &(event->start.time.date.year),
2089                                                 &(event->start.time.date.month), &(event->start.time.date.mday),
2090                                                 &(event->start.time.date.hour), &(event->start.time.date.minute),
2091                                                 &(event->start.time.date.second));
2092                         }
2093                 }
2094                 break;
2095         case CAL_PROPERTY_EVENT_START_TZID:
2096                 temp = sqlite3_column_text(stmt, *stmt_count);
2097                 event->start_tzid = cal_strdup((const char*)temp);
2098                 break;
2099         case CAL_PROPERTY_EVENT_END:
2100                 event->end.type = sqlite3_column_int(stmt, *stmt_count);
2101                 if (event->end.type == CALENDAR_TIME_UTIME) {
2102                         *stmt_count = *stmt_count+1;
2103                         event->end.time.utime = sqlite3_column_int64(stmt, *stmt_count);
2104                         *stmt_count = *stmt_count+1; /* dtstart_datetime */
2105                 } else {
2106                         *stmt_count = *stmt_count+1; /* dtend_utime */
2107                         *stmt_count = *stmt_count+1;
2108                         temp = sqlite3_column_text(stmt, *stmt_count);
2109                         if (temp) {
2110                                 sscanf((const char *)temp, CAL_FORMAT_LOCAL_DATETIME, &(event->end.time.date.year),
2111                                                 &(event->end.time.date.month), &(event->end.time.date.mday),
2112                                                 &(event->end.time.date.hour), &(event->end.time.date.minute),
2113                                                 &(event->end.time.date.second));
2114                         }
2115                 }
2116                 break;
2117         case CAL_PROPERTY_EVENT_END_TZID:
2118                 temp = sqlite3_column_text(stmt, *stmt_count);
2119                 event->end_tzid = cal_strdup((const char*)temp);
2120                 break;
2121         case CAL_PROPERTY_EVENT_CALENDAR_SYSTEM_TYPE:
2122                 event->system_type = sqlite3_column_int(stmt, *stmt_count);
2123                 break;
2124         case CAL_PROPERTY_EVENT_IS_ALLDAY:
2125                 event->is_allday = sqlite3_column_int(stmt, *stmt_count);
2126                 break;
2127         default:
2128                 /* LCOV_EXCL_START */
2129                 ERR("invalid (0x%x)", property);
2130                 break;
2131                 /* LCOV_EXCL_STOP */
2132         }
2133
2134         *stmt_count = *stmt_count+1;
2135 }
2136
2137 static void _cal_db_event_get_projection_stmt(sqlite3_stmt *stmt,
2138                 const unsigned int *projection, const int projection_count,
2139                 calendar_record_h record)
2140 {
2141         int i = 0;
2142         int stmt_count = 0;
2143
2144         for (i = 0; i < projection_count; i++)
2145                 _cal_db_event_get_property_stmt(stmt, projection[i], &stmt_count, record);
2146 }
2147
2148 static bool _cal_db_event_check_calendar_book_type(calendar_record_h record)
2149 {
2150         int ret = 0;
2151         int store_type = 0;
2152         cal_event_s *event = (cal_event_s *)record;
2153         char query[CAL_DB_SQL_MAX_LEN];
2154         sqlite3_stmt *stmt = NULL;
2155
2156         snprintf(query, sizeof(query), "SELECT store_type FROM %s WHERE id = %d ",
2157                         CAL_TABLE_CALENDAR, event->calendar_id);
2158         ret = cal_db_util_query_prepare(query, &stmt);
2159         if (CALENDAR_ERROR_NONE != ret) {
2160                 /* LCOV_EXCL_START */
2161                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
2162                 SECURE("query[%s]", query);
2163                 return false;
2164                 /* LCOV_EXCL_STOP */
2165         }
2166
2167         if (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
2168                 store_type = sqlite3_column_int(stmt, 0);
2169         } else {
2170                 /* LCOV_EXCL_START */
2171                 ERR("Failed to get calendar: calendar_id(%d)", event->calendar_id);
2172                 sqlite3_finalize(stmt);
2173                 stmt = NULL;
2174                 return false;
2175                 /* LCOV_EXCL_STOP */
2176         }
2177         sqlite3_finalize(stmt);
2178
2179         switch (store_type) {
2180         case CALENDAR_BOOK_TYPE_NONE:
2181         case CALENDAR_BOOK_TYPE_EVENT:
2182                 ret = true;
2183                 break;
2184         case CALENDAR_BOOK_TYPE_TODO:
2185         default:
2186                 ret = false;
2187                 break;
2188         }
2189         return ret;
2190 }
2191
2192 static int _cal_db_event_delete_exception(int *exception_ids, int exception_len)
2193 {
2194         int ret = 0;
2195         char query[CAL_DB_SQL_MAX_LEN] = {0};
2196
2197         int i;
2198         for (i = 0; i < exception_len; i++) {
2199                 snprintf(query, sizeof(query), "DELETE FROM %s WHERE id=%d ", CAL_TABLE_SCHEDULE, exception_ids[i]);
2200                 ret = cal_db_util_query_exec(query);
2201                 if (CALENDAR_ERROR_NONE != ret) {
2202                         /* LCOV_EXCL_START */
2203                         ERR("cal_db_util_query_exec() Fail(%d)", ret);
2204                         SECURE("[%s]", query);
2205                         return ret;
2206                         /* LCOV_EXCL_STOP */
2207                 }
2208         }
2209         return CALENDAR_ERROR_NONE;
2210 }
2211
2212 static int _cal_db_event_update_dirty(calendar_record_h record, int is_dirty_in_time)
2213 {
2214         int event_id = 0;
2215         int ret = CALENDAR_ERROR_NONE;
2216         calendar_record_h original_record = NULL;
2217
2218         ret = calendar_record_get_int(record, _calendar_event.id, &event_id);
2219         RETV_IF(CALENDAR_ERROR_NONE != ret, ret);
2220
2221         DBG("id=%d", event_id);
2222
2223         ret = _cal_db_event_get_record(event_id, &original_record);
2224         RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "_cal_db_event_get_record() Fail(%d)", ret);
2225
2226         cal_record_s *_record = NULL;
2227         const cal_property_info_s* property_info = NULL;
2228         int property_info_count = 0;
2229         int i = 0;
2230
2231         _record = (cal_record_s *)record;
2232
2233         property_info = cal_view_get_property_info(_record->view_uri, &property_info_count);
2234
2235         for (i = 0; i < property_info_count; i++) {
2236                 if (false == cal_record_check_property_flag(record, property_info[i].property_id , CAL_PROPERTY_FLAG_DIRTY))
2237                         continue;
2238
2239                 if (property_info[i].property_id == CAL_PROPERTY_EVENT_EXDATE) {
2240                         int calendar_id = 0;
2241                         int account_id = 0;
2242                         calendar_book_sync_event_type_e sync_event_type = CALENDAR_BOOK_SYNC_EVENT_FOR_ME;
2243
2244                         char *record_exdate = NULL;
2245                         ret = calendar_record_get_str_p(record, property_info[i]. property_id, &record_exdate);
2246                         if (CALENDAR_ERROR_NONE != ret)
2247                                 continue;
2248                         ret = calendar_record_get_int(original_record, CAL_PROPERTY_EVENT_BOOK_ID, &calendar_id);
2249                         if (CALENDAR_ERROR_NONE != ret)
2250                                 continue;
2251
2252                         calendar_record_h record_calendar = NULL;
2253                         ret = cal_db_get_record(_calendar_book._uri, calendar_id, &record_calendar);
2254                         ret |= calendar_record_get_int(record_calendar, _calendar_book.account_id, &account_id);
2255                         ret |= calendar_record_get_int(record_calendar, _calendar_book.sync_event, (int *)&sync_event_type);
2256                         DBG("calendar_id(%d), account_id(%d), sync_event(%d)", calendar_id, account_id, sync_event_type);
2257                         calendar_record_destroy(record_calendar, true);
2258
2259                         char *original_exdate = NULL;
2260                         ret = calendar_record_get_str_p(original_record, property_info[i].property_id, &original_exdate);
2261                         if (CALENDAR_ERROR_NONE != ret)
2262                                 continue;
2263                         if (sync_event_type == CALENDAR_BOOK_SYNC_EVENT_FOR_EVERY_AND_REMAIN) {
2264                                 ret = _cal_db_event_exdate_insert_utime(event_id, original_exdate,
2265                                                 record_exdate, NULL, NULL);
2266                                 WARN_IF(CALENDAR_ERROR_NONE != ret, "%s->%s", original_exdate, record_exdate);
2267
2268                         } else {
2269                                 int *exception_ids = NULL;
2270                                 int exception_len = 0;
2271                                 ret = _cal_db_event_exdate_insert_utime(event_id, original_exdate,
2272                                                 record_exdate, &exception_ids, &exception_len);
2273                                 WARN_IF(CALENDAR_ERROR_NONE != ret, "%s->%s", original_exdate, record_exdate);
2274                                 ret = _cal_db_event_delete_exception(exception_ids, exception_len);
2275                                 WARN_IF(CALENDAR_ERROR_NONE != ret, "_cal_db_event_delete_record() Fail");
2276                                 free(exception_ids);
2277                         }
2278                         ret = cal_record_set_str(original_record, property_info[i].property_id, record_exdate);
2279                         if (CALENDAR_ERROR_NONE != ret)
2280                                 continue;
2281                 } else if (CAL_PROPERTY_CHECK_DATA_TYPE(property_info[i].property_id, CAL_PROPERTY_DATA_TYPE_INT) == true) {
2282                         int tmp = 0;
2283                         ret = calendar_record_get_int(record, property_info[i].property_id, &tmp);
2284                         if (CALENDAR_ERROR_NONE != ret)
2285                                 continue;
2286                         ret = cal_record_set_int(original_record, property_info[i].property_id, tmp);
2287                         if (CALENDAR_ERROR_NONE != ret)
2288                                 continue;
2289                 } else if (CAL_PROPERTY_CHECK_DATA_TYPE(property_info[i].property_id, CAL_PROPERTY_DATA_TYPE_STR) == true) {
2290                         char *tmp = NULL;
2291                         ret = calendar_record_get_str_p(record, property_info[i].property_id, &tmp);
2292                         if (CALENDAR_ERROR_NONE != ret)
2293                                 continue;
2294                         ret = cal_record_set_str(original_record, property_info[i].property_id, tmp);
2295                         if (CALENDAR_ERROR_NONE != ret)
2296                                 continue;
2297                 } else if (CAL_PROPERTY_CHECK_DATA_TYPE(property_info[i].property_id, CAL_PROPERTY_DATA_TYPE_DOUBLE) == true) {
2298                         double tmp = 0;
2299                         ret = calendar_record_get_double(record, property_info[i].property_id, &tmp);
2300                         if (CALENDAR_ERROR_NONE != ret)
2301                                 continue;
2302                         ret = cal_record_set_double(original_record, property_info[i].property_id, tmp);
2303                         if (CALENDAR_ERROR_NONE != ret)
2304                                 continue;
2305                 } else if (CAL_PROPERTY_CHECK_DATA_TYPE(property_info[i].property_id, CAL_PROPERTY_DATA_TYPE_LLI) == true) {
2306                         long long int tmp = 0;
2307                         ret = calendar_record_get_lli(record, property_info[i].property_id, &tmp);
2308                         if (CALENDAR_ERROR_NONE != ret)
2309                                 continue;
2310                         ret = cal_record_set_lli(original_record, property_info[i].property_id, tmp);
2311                         if (CALENDAR_ERROR_NONE != ret)
2312                                 continue;
2313                 } else if (CAL_PROPERTY_CHECK_DATA_TYPE(property_info[i].property_id, CAL_PROPERTY_DATA_TYPE_CALTIME) == true) {
2314                         calendar_time_s tmp = {0,};
2315                         ret = calendar_record_get_caltime(record, property_info[i].property_id, &tmp);
2316                         if (CALENDAR_ERROR_NONE != ret)
2317                                 continue;
2318                         ret = cal_record_set_caltime(original_record, property_info[i].property_id, tmp);
2319                         if (CALENDAR_ERROR_NONE != ret)
2320                                 continue;
2321                 }
2322         }
2323         /* child replace */
2324         cal_event_s *tmp = (cal_event_s *)original_record;
2325         cal_event_s *tmp_src = (cal_event_s *)record;
2326
2327         if (tmp->alarm_list)
2328                 calendar_list_destroy((calendar_list_h)tmp->alarm_list, true);
2329         cal_list_clone((calendar_list_h)tmp_src->alarm_list, (calendar_list_h *)&tmp->alarm_list);
2330
2331         if (tmp->attendee_list)
2332                 calendar_list_destroy((calendar_list_h)tmp->attendee_list, true);
2333         cal_list_clone((calendar_list_h)tmp_src->attendee_list, (calendar_list_h *)&tmp->attendee_list);
2334
2335         if (tmp->exception_list)
2336                 calendar_list_destroy((calendar_list_h)tmp->exception_list, true);
2337         cal_list_clone((calendar_list_h)tmp_src->exception_list, (calendar_list_h *)&tmp->exception_list);
2338
2339         if (tmp->extended_list)
2340                 calendar_list_destroy((calendar_list_h)tmp->extended_list, true);
2341         cal_list_clone((calendar_list_h)tmp_src->extended_list, (calendar_list_h *)&tmp->extended_list);
2342
2343         CAL_RECORD_RESET_COMMON((cal_record_s*)original_record);
2344         ret = __update_record(original_record, is_dirty_in_time);
2345         calendar_record_destroy(original_record, true);
2346
2347         return ret;
2348 }
2349
2350 static int _cal_db_event_exception_get_records(int original_id, cal_list_s *list)
2351 {
2352         int ret;
2353         char query[CAL_DB_SQL_MAX_LEN] = {0};
2354         sqlite3_stmt *stmt = NULL;
2355
2356         RETVM_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER,
2357                         "Invalid parameter: list is NULL");
2358
2359         snprintf(query, sizeof(query),
2360                         "SELECT "CAL_QUERY_SCHEDULE_A_ALL" FROM %s AS A "
2361                         "WHERE original_event_id = %d AND is_deleted = 0 ",
2362                         CAL_TABLE_SCHEDULE,
2363                         original_id);
2364
2365         ret = cal_db_util_query_prepare(query, &stmt);
2366         if (CALENDAR_ERROR_NONE != ret) {
2367                 /* LCOV_EXCL_START */
2368                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
2369                 SECURE("query[%s]", query);
2370                 return ret;
2371                 /* LCOV_EXCL_STOP */
2372         }
2373
2374         calendar_record_h record = NULL;
2375
2376         while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
2377                 int exception = 0, extended = 0;
2378                 ret = calendar_record_create(_calendar_event._uri, &record);
2379                 if (CALENDAR_ERROR_NONE != ret) {
2380                         /* LCOV_EXCL_START */
2381                         ERR("calendar_record_create() Fail(%d)", ret);
2382                         sqlite3_finalize(stmt);
2383                         cal_list_clear(list);
2384                         return ret;
2385                         /* LCOV_EXCL_STOP */
2386                 }
2387
2388                 _cal_db_event_get_stmt(stmt, false, record, &exception, &extended);
2389
2390                 cal_rrule_s *rrule = NULL;
2391                 cal_event_s *event = (cal_event_s *)record;
2392                 if (CALENDAR_RECURRENCE_NONE != event->freq) {
2393                         ret = cal_db_rrule_get_rrule(event->index, &rrule);
2394                         if (CALENDAR_ERROR_NONE == ret) {
2395                                 cal_db_rrule_set_rrule_to_record(rrule, record);
2396                                 CAL_FREE(rrule);
2397                         }
2398                 }
2399
2400                 if (event->has_alarm == 1)
2401                         cal_db_alarm_get_records(event->index, event->alarm_list);
2402
2403                 if (event->has_attendee == 1)
2404                         cal_db_attendee_get_records(event->index, event->attendee_list);
2405
2406                 if (exception == 1)
2407                         _cal_db_event_exception_get_records(event->index, event->exception_list);
2408
2409                 if (extended == 1)
2410                         cal_db_extended_get_records(event->index, CALENDAR_RECORD_TYPE_EVENT, event->extended_list);
2411
2412                 event->has_alarm = 0;
2413                 if (event->alarm_list && 0 < event->alarm_list->count)
2414                         event->has_alarm = 1;
2415
2416                 event->has_attendee = 0;
2417                 if (event->attendee_list && 0 < event->attendee_list->count)
2418                         event->has_attendee = 1;
2419
2420                 calendar_list_add((calendar_list_h)list, record);
2421         }
2422         sqlite3_finalize(stmt);
2423         return CALENDAR_ERROR_NONE;
2424 }
2425
2426 static int _cal_db_event_exception_delete_with_id(int original_id)
2427 {
2428         int ret = 0;
2429         char query[CAL_DB_SQL_MAX_LEN] = {0};
2430
2431         DBG("delete exception mod with original event id(%d)", original_id);
2432         snprintf(query, sizeof(query), "DELETE FROM %s WHERE original_event_id=%d ",
2433                         CAL_TABLE_SCHEDULE, original_id);
2434         ret = cal_db_util_query_exec(query);
2435         if (CALENDAR_ERROR_NONE != ret) {
2436                 /* LCOV_EXCL_START */
2437                 ERR("cal_db_util_query_exec() Fail(%d)", ret);
2438                 SECURE("[%s]", query);
2439                 return ret;
2440                 /* LCOV_EXCL_STOP */
2441         }
2442
2443         return CALENDAR_ERROR_NONE;
2444 }
2445
2446 static int _cal_db_event_exception_get_ids(int original_id, GList **out_list)
2447 {
2448         int ret = 0;
2449         char query[CAL_DB_SQL_MAX_LEN] = {0};
2450         sqlite3_stmt *stmt = NULL;
2451         GList *list = NULL;
2452
2453         RETV_IF(NULL == out_list, CALENDAR_ERROR_INVALID_PARAMETER);
2454
2455         snprintf(query, sizeof(query), "SELECT id FROM %s WHERE original_event_id = %d AND is_deleted = 0 ",
2456                         CAL_TABLE_SCHEDULE, original_id);
2457
2458         ret = cal_db_util_query_prepare(query, &stmt);
2459         if (CALENDAR_ERROR_NONE != ret) {
2460                 /* LCOV_EXCL_START */
2461                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
2462                 SECURE("query[%s]", query);
2463                 return ret;
2464                 /* LCOV_EXCL_STOP */
2465         }
2466
2467         while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
2468                 int id = 0;
2469                 id = sqlite3_column_int(stmt, 0);
2470                 list = g_list_append(list, GINT_TO_POINTER(id));
2471
2472         }
2473         sqlite3_finalize(stmt);
2474
2475         *out_list = list;
2476         return CALENDAR_ERROR_NONE;
2477 }
2478
2479 static int _cal_db_event_exception_update(cal_list_s *exception_list_s, int original_id, int calendar_id, int is_dirty_in_time, time_t time_diff, int old_type, int new_type)
2480 {
2481         int count = 0;
2482         int ret = CALENDAR_ERROR_NONE;
2483         calendar_record_h exception = NULL;
2484         calendar_list_h exception_list = (calendar_list_h)exception_list_s;
2485         GList *id_list = NULL;
2486
2487         _cal_db_event_exception_get_ids(original_id, &id_list);
2488
2489         if (exception_list) {
2490                 calendar_list_get_count(exception_list, &count);
2491                 calendar_list_first(exception_list);
2492                 while (CALENDAR_ERROR_NONE == calendar_list_get_current_record_p(exception_list, &exception)) {
2493                         int exception_id = 0;
2494                         ret = calendar_record_get_int(exception, _calendar_event.id, &exception_id);
2495                         DBG("exception(%d)", exception_id);
2496                         if (0 < exception_id && exception_id != original_id) { /* update */
2497                                 bool bchanged = false;
2498                                 cal_record_s *_record = NULL;
2499                                 const cal_property_info_s* property_info = NULL;
2500                                 int property_info_count = 0;
2501
2502                                 _record = (cal_record_s *)exception;
2503
2504                                 property_info = cal_view_get_property_info(_record->view_uri, &property_info_count);
2505
2506                                 /* check updated */
2507                                 int i;
2508                                 for (i = 0; i < property_info_count; i++) {
2509                                         if (true == cal_record_check_property_flag(exception, property_info[i].property_id , CAL_PROPERTY_FLAG_DIRTY)) {
2510                                                 bchanged = true;
2511                                                 break;
2512                                         }
2513                                 }
2514
2515                                 if (bchanged == true || DIRTY_IN_TIME == is_dirty_in_time) {
2516                                         /* update */
2517                                         __update_recurrence_id(exception, old_type, new_type, time_diff);
2518                                         ret = _cal_db_event_update_record(exception);
2519                                         cal_db_instance_discard_record(exception_id);
2520                                         cal_db_instance_publish_record(exception);
2521
2522                                 } else {
2523                                         cal_db_instance_discard_record(exception_id);
2524                                         cal_db_instance_publish_record(exception);
2525                                         DBG("exception don't changed. exception_id=[%d]", exception_id);
2526                                 }
2527
2528                                 if (id_list)
2529                                         id_list = g_list_remove(id_list, GINT_TO_POINTER(exception_id));
2530
2531                         } else {
2532                                 /* insert */
2533                                 ret = cal_record_set_int(exception, _calendar_event.calendar_book_id, calendar_id);
2534                                 if (CALENDAR_ERROR_NONE != ret) {
2535                                         /* LCOV_EXCL_START */
2536                                         ERR("cal_record_set_int() Fail(%d)", ret);
2537                                         if (id_list)
2538                                                 g_list_free(id_list);
2539                                         return ret;
2540                                         /* LCOV_EXCL_STOP */
2541                                 }
2542                                 ret = cal_db_event_insert_record(exception, original_id, NULL);
2543                                 if (CALENDAR_ERROR_NONE != ret) {
2544                                         /* LCOV_EXCL_START */
2545                                         ERR("cal_db_event_insert_record() Fail(%d)", ret);
2546                                         if (id_list)
2547                                                 g_list_free(id_list);
2548                                         return ret;
2549                                         /* LCOV_EXCL_STOP */
2550                                 }
2551                         }
2552                         calendar_list_next(exception_list);
2553                 }
2554         }
2555
2556         if (id_list) {
2557                 GList * tmp_list = g_list_first(id_list);
2558                 while (tmp_list) {
2559                         int tmp = GPOINTER_TO_INT(tmp_list->data);
2560                         char query[CAL_DB_SQL_MAX_LEN] = {0};
2561
2562                         snprintf(query, sizeof(query), "DELETE FROM %s WHERE id=%d ", CAL_TABLE_SCHEDULE, tmp);
2563                         ret = cal_db_util_query_exec(query);
2564                         if (CALENDAR_ERROR_NONE != ret) {
2565                                 /* LCOV_EXCL_START */
2566                                 WARN("cal_db_util_query_exec() Fail(%d)", ret);
2567                                 SECURE("[%s]", query);
2568                                 /* LCOV_EXCL_STOP */
2569                         }
2570                         tmp_list = g_list_next(tmp_list);
2571                 }
2572                 g_list_free(id_list);
2573         }
2574         return CALENDAR_ERROR_NONE;
2575 }
2576
2577
2578 static int _cal_db_event_get_deleted_data(int id, int* book_id, int* created_ver,
2579                 int* original_event_id, char** recurrence_id)
2580 {
2581         int ret = CALENDAR_ERROR_NONE;
2582         char query[CAL_DB_SQL_MAX_LEN] = {0};
2583         sqlite3_stmt *stmt = NULL;
2584
2585         RETV_IF(NULL == book_id, CALENDAR_ERROR_INVALID_PARAMETER);
2586         RETV_IF(NULL == created_ver, CALENDAR_ERROR_INVALID_PARAMETER);
2587         RETV_IF(NULL == original_event_id, CALENDAR_ERROR_INVALID_PARAMETER);
2588         RETV_IF(NULL == recurrence_id, CALENDAR_ERROR_INVALID_PARAMETER);
2589
2590         snprintf(query, sizeof(query), "SELECT calendar_id, created_ver, "
2591                         "original_event_id, recurrence_id FROM %s WHERE id=%d ",
2592                         CAL_TABLE_SCHEDULE, id);
2593
2594         ret = cal_db_util_query_prepare(query, &stmt);
2595         if (CALENDAR_ERROR_NONE != ret) {
2596                 /* LCOV_EXCL_START */
2597                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
2598                 SECURE("query[%s]", query);
2599                 return ret;
2600                 /* LCOV_EXCL_STOP */
2601         }
2602
2603         ret = cal_db_util_stmt_step(stmt);
2604         if (CAL_SQLITE_ROW != ret) {
2605                 /* LCOV_EXCL_START */
2606                 ERR("cal_db_util_stmt_step() Fail(%d)", ret);
2607                 SECURE("query[%s]", query);
2608                 sqlite3_finalize(stmt);
2609                 return CALENDAR_ERROR_DB_RECORD_NOT_FOUND;
2610                 /* LCOV_EXCL_STOP */
2611         }
2612
2613         *book_id = sqlite3_column_int(stmt, 0);
2614         *created_ver = sqlite3_column_int(stmt, 1);
2615         *original_event_id = sqlite3_column_int(stmt, 2);
2616         *recurrence_id = cal_strdup((const char *)sqlite3_column_text(stmt, 3));
2617
2618         sqlite3_finalize(stmt);
2619
2620         return CALENDAR_ERROR_NONE;
2621 }
2622
2623 static int _cal_db_event_exdate_insert_utime(int event_id, const char* original_exdate,
2624                 const char* exdate, int **exception_ids, int *exception_len)
2625 {
2626         int ret = CALENDAR_ERROR_NONE;
2627         gchar **patterns1 = NULL;
2628         gchar **patterns2 = NULL;
2629         int len1 = 0, len2 = 0, i = 0, j = 0;
2630
2631         int input_ver = cal_db_util_get_next_ver();
2632         if (exdate != NULL && 0 < strlen(exdate)) {
2633                 patterns1 = g_strsplit_set(exdate, " ,", -1);
2634                 len1 = g_strv_length(patterns1);
2635         }
2636         if (original_exdate && 0 < strlen(original_exdate)) {
2637                 patterns2 = g_strsplit_set(original_exdate, " ,", -1);
2638                 len2 = g_strv_length(patterns2);
2639         }
2640
2641         int *ids = calloc(len1, sizeof(int));
2642         RETVM_IF(NULL == ids, CALENDAR_ERROR_OUT_OF_MEMORY, "calloc() Fail");
2643
2644         int exception_count = 0;
2645         for (i = 0; i < len1; i++) {
2646                 if (NULL == patterns1[i]) {
2647                         WARN("exdate is NULL, so check next");
2648                         continue;
2649                 }
2650                 bool bFind = false;
2651                 for (j = 0; j < len2; j++) {
2652                         if (NULL == patterns2[j]) {
2653                                 WARN("original exdate is NULL");
2654                                 continue;
2655                         }
2656                         if (CAL_STRING_EQUAL == strcmp(patterns1[i], patterns2[j])) {
2657                                 bFind = true;
2658                                 break;
2659                         }
2660                 }
2661                 if (bFind == false) {
2662                         char query[CAL_DB_SQL_MAX_LEN] = {0};
2663                         long long int start_utime = 0;
2664                         char datetime[16] = {0};
2665                         if (strlen("YYYYMMDD") < strlen(patterns1[i])) {
2666                                 int y, mon, d, h, min, s;
2667                                 sscanf(patterns1[i], CAL_DATETIME_FORMAT_YYYYMMDDTHHMMSSZ,
2668                                                 &y, &mon, &d, &h, &min, &s);
2669                                 start_utime = cal_time_convert_itol(NULL, y, mon, d, h, min, s);
2670
2671                         } else {
2672                                 snprintf(datetime, sizeof(datetime),
2673                                                 "%s", patterns1[i]);
2674
2675                         }
2676                         snprintf(query, sizeof(query),
2677                                         "INSERT INTO %s ("
2678                                         "type, "
2679                                         "created_ver, changed_ver, "
2680                                         "calendar_id, "
2681                                         "original_event_id, "
2682                                         "recurrence_id, "
2683                                         "is_deleted, "
2684                                         "dtstart_type, "
2685                                         "dtstart_utime, "
2686                                         "dtstart_datetime, "
2687                                         "dtstart_tzid, "
2688                                         "dtend_type, "
2689                                         "dtend_utime, "
2690                                         "dtend_datetime, "
2691                                         "dtend_tzid"
2692                                         ") SELECT %d,"
2693                                         "created_ver, %d, "
2694                                         "calendar_id, "
2695                                         "%d, "
2696                                         "'%s', "
2697                                         "1, "
2698                                         "dtstart_type, "
2699                                         "%lld, "
2700                                         "'%s', "
2701                                         "dtstart_tzid, "
2702                                         "dtend_type, "
2703                                         "%lld+(dtend_utime-dtstart_utime), "
2704                                         "'%s', "
2705                                         "dtend_tzid "
2706                                         "FROM %s "
2707                                         "WHERE id = %d; ",
2708                                 CAL_TABLE_SCHEDULE,
2709                                 CAL_SCH_TYPE_EVENT,
2710                                 input_ver,
2711                                 event_id,
2712                                 patterns1[i],
2713                                 start_utime,
2714                                 datetime,
2715                                 start_utime,
2716                                 datetime,
2717                                 CAL_TABLE_SCHEDULE,
2718                                 event_id
2719                                         );
2720                         ret = cal_db_util_query_exec(query);
2721                         if (CALENDAR_ERROR_NONE != ret) {
2722                                 /* LCOV_EXCL_START */
2723                                 WARN("cal_db_util_query_exec() Fail(%d)", ret);
2724                                 SECURE("[%s]", query);
2725                                 /* LCOV_EXCL_STOP */
2726                         }
2727                         int event_id = cal_db_util_last_insert_id();
2728                         DBG("last id(%d)", event_id);
2729                         ids[exception_count] = event_id;
2730                         exception_count++;
2731                 }
2732         }
2733         if (exception_ids)
2734                 *exception_ids = ids;
2735         else
2736                 free(ids);
2737
2738         if (exception_len)
2739                 *exception_len = exception_count;
2740
2741         g_strfreev(patterns1);
2742         g_strfreev(patterns2);
2743         return ret;
2744 }