4 * Copyright (c) 2012 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
22 #include "calendar_db.h"
23 #include "cal_internal.h"
24 #include "cal_typedef.h"
27 #include "cal_record.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"
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);
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);
69 static void __check_list(calendar_list_h l)
71 DBG("---------------------");
72 calendar_list_first(l);
74 calendar_list_get_count(l, &count);
75 DBG("count(%d)", count);
78 for (i = 0; i < count; i++) {
79 calendar_record_h event = NULL;
80 calendar_list_get_current_record_p(l, &event);
82 calendar_record_get_int(event, _calendar_event.id, &id);
84 calendar_record_get_int(event, _calendar_event.calendar_book_id, &book_id);
86 calendar_record_get_str_p(event, _calendar_event.summary, &summary);
88 calendar_record_get_int(event, _calendar_event.freq, &freq);
90 DBG("id(%d) book_id(%d) summary[%s] freq(%d)", id, book_id, summary, freq);
91 calendar_list_next(l);
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
112 static int _cal_db_event_insert_record(calendar_record_h record, int* id)
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);
126 static int _cal_db_event_get_record(int id, calendar_record_h* out_record)
128 char query[CAL_DB_SQL_MAX_LEN];
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;
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;
145 event = (cal_event_s*)(*out_record);
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)",
152 id, CALENDAR_BOOK_TYPE_EVENT, CALENDAR_BOOK_TYPE_NONE,
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);
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);
173 if (CALENDAR_ERROR_NONE == ret)
174 return CALENDAR_ERROR_DB_RECORD_NOT_FOUND;
179 _cal_db_event_get_stmt(stmt, false, *out_record, &exception, &extended);
180 sqlite3_finalize(stmt);
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);
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);
194 return CALENDAR_ERROR_DB_RECORD_NOT_FOUND;
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);
205 if (event->has_alarm == 1)
206 cal_db_alarm_get_records(event->index, event->alarm_list);
208 if (event->has_attendee == 1)
209 cal_db_attendee_get_records(event->index, event->attendee_list);
212 _cal_db_event_exception_get_records(event->index, event->exception_list);
215 cal_db_extended_get_records(event->index, CALENDAR_RECORD_TYPE_EVENT, event->extended_list);
217 return CALENDAR_ERROR_NONE;
220 static int __is_dirty_in_time(calendar_record_h record)
222 cal_record_s *rec = (cal_record_s *)record;
223 RETV_IF(NULL == record, false);
224 RETV_IF(NULL == rec->view_uri, false);
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");
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))
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:
257 is_dirty_in_time = DIRTY_IN_TIME;
260 if (DIRTY_IN_OTHER != is_dirty_in_time) break;
262 DBG("%sirty in time", DIRTY_IN_OTHER == is_dirty_in_time ? "Not d" : "D");
263 return is_dirty_in_time;
266 time_t __get_time_diff(char *old_tzid, calendar_time_s *old, char *new_tzid, calendar_time_s *new)
268 RETV_IF(NULL == old, CALENDAR_ERROR_INVALID_PARAMETER);
269 RETV_IF(NULL == new, CALENDAR_ERROR_INVALID_PARAMETER);
273 case CALENDAR_TIME_UTIME:
275 case CALENDAR_TIME_UTIME:
276 DBG("%lld - %lld", old->time.utime, new->time.utime);
277 diff = old->time.utime - new->time.utime;
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);
287 case CALENDAR_TIME_LOCALTIME:
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;
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);
305 DBG("-------------time diff(%ld)", diff);
309 static int __get_time_shifted_field(char *old_field, int old_type, int new_type, time_t time_diff, char **new_field)
311 if (NULL == old_field || '\0' == *old_field)
312 return CALENDAR_ERROR_NONE;
315 t = g_strsplit_set(old_field, " ,", -1);
316 RETVM_IF(NULL == t, CALENDAR_ERROR_DB_FAILED, "g_strsplit_set() Fail");
318 int len_t = g_strv_length(t);
321 int len_field = strlen(old_field);
322 len_field += (len_t * 8) + 1; /* add (len_t * 8) for YYYYMMDD -> YYYYMMDDTHHMMSSZ */
324 field = calloc(len_field, sizeof(char)); /* add (len_t * 8) for YYYYMMDD -> YYYYMMDDTHHMMSSZ */
326 /* LCOV_EXCL_START */
327 ERR("calloc() Fail");
329 return CALENDAR_ERROR_OUT_OF_MEMORY;
338 for (i = 0; i < len_t; i++) {
339 int y = 0, m = 0, d = 0;
340 int h = 0, n = 0, s = 0;
342 case CALENDAR_TIME_UTIME:
343 sscanf(t[i], CAL_DATETIME_FORMAT_YYYYMMDDTHHMMSSZ, &y, &m, &d, &h, &n, &s);
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);
352 case 15: /* YYYYMMDDTHHMMSS */
353 sscanf(t[i], CAL_DATETIME_FORMAT_YYYYMMDDTHHMMSS, &y, &m, &d, &h, &n, &s);
359 tm.tm_year = y - 1900;
366 char buf[CAL_STR_SHORT_LEN32] = {0};
368 case CALENDAR_TIME_UTIME:
369 switch (strlen(t[i])) {
370 case 8: /* YYYYMMDD */
371 case 15: /* YYYYMMDDTHHMMSS */
372 tt = timelocal(&tm) - time_diff;
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);
379 case 16: /* YYYYMMDDTHHMMSSZ */
380 tt = timegm(&tm) - time_diff;
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);
390 case CALENDAR_TIME_LOCALTIME:
391 switch (strlen(t[i])) {
392 case 8: /* YYYYMMDD */
393 tt = timegm(&tm) - time_diff;
395 snprintf(buf, sizeof(buf), "%s"CAL_DATETIME_FORMAT_YYYYMMDD,
396 i == 0 ? "" : ",", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
398 case 15: /* YYYYMMDDTHHMMSS */
399 tt = timegm(&tm) - time_diff;
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);
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);
415 len += snprintf(field + len, len_field -len, "%s", buf);
425 return CALENDAR_ERROR_NONE;
428 static int __update_exdate(cal_event_s *rec, time_t time_diff)
430 RETV_IF(NULL == rec, CALENDAR_ERROR_INVALID_PARAMETER);
432 if (NULL == rec->exdate || '\0' == *rec->exdate)
433 return CALENDAR_ERROR_NONE;
435 return CALENDAR_ERROR_NONE;
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);
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);
447 char *new_exdate = NULL;
448 __get_time_shifted_field(rec->exdate, ct.type, rec->start.type, time_diff, &new_exdate);
450 rec->exdate = new_exdate;
451 DBG("new exdate[%s]", new_exdate);
453 calendar_record_destroy(db_record, true);
455 return CALENDAR_ERROR_NONE;
458 static int __update_recurrence_id(calendar_record_h exception, int old_type, int new_type, time_t time_diff)
462 cal_event_s *rec = (cal_event_s *)exception;
463 RETV_IF(NULL == rec, CALENDAR_ERROR_INVALID_PARAMETER);
465 if (NULL == rec->recurrence_id) {
466 DBG("No recurrence_id");
467 return CALENDAR_ERROR_NONE;
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);
476 return CALENDAR_ERROR_NONE;
479 static int __update_record(calendar_record_h record, int is_dirty_in_time)
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;
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);
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);
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;
512 if (event->common.properties_flags) {
513 if (DIRTY_INIT == is_dirty_in_time)
514 is_dirty_in_time = __is_dirty_in_time(record);
516 return _cal_db_event_update_dirty(record, is_dirty_in_time);
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);
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);
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();
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)) {
553 snprintf(query, sizeof(query), "UPDATE %s SET "
568 "organizer_name = ?, "
569 "organizer_email = ?, "
570 "meeting_status = %d, "
572 "original_event_id = %d,"
576 "completed_time = %lld,"
578 "dtstart_type = %d, "
579 "dtstart_utime = %lld, "
580 "dtstart_datetime = ?, "
583 "dtend_utime = %lld, "
584 "dtend_datetime = ?, "
586 "last_mod = strftime('%%s', 'now'), "
588 "recurrence_id = ?, "
590 "has_attendee = %d, "
598 "has_exception = %d, "
599 "has_extended = %d, "
605 CAL_SCH_TYPE_EVENT,/*event->cal_type,*/
608 event->timezone ? event->timezone : timezone_id,
612 event->meeting_status,
614 event->original_event_id,
618 (long long int)0, /* event->completed_time */
619 0, /* event->progress, */
621 event->start.type == CALENDAR_TIME_UTIME ? event->start.time.utime : 0,
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,
629 (event->exception_list && 0 < event->exception_list->count) ? 1 : 0,
630 (event->extended_list && 0 < event->extended_list->count) ? 1 : 0,
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);
647 cal_db_util_stmt_bind_text(stmt, index, event->summary);
650 if (event->description)
651 cal_db_util_stmt_bind_text(stmt, index, event->description);
655 cal_db_util_stmt_bind_text(stmt, index, event->location);
658 if (event->categories)
659 cal_db_util_stmt_bind_text(stmt, index, event->categories);
663 if (DIRTY_IN_TIME == is_dirty_in_time)
664 __update_exdate(event, time_diff);
666 cal_db_util_stmt_bind_text(stmt, index, event->exdate);
671 cal_db_util_stmt_bind_text(stmt, index, event->uid);
674 if (event->organizer_name)
675 cal_db_util_stmt_bind_text(stmt, index, event->organizer_name);
678 if (event->organizer_email)
679 cal_db_util_stmt_bind_text(stmt, index, event->organizer_email);
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);
694 if (event->start_tzid)
695 cal_db_util_stmt_bind_text(stmt, index, event->start_tzid);
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);
711 cal_db_util_stmt_bind_text(stmt, index, event->end_tzid);
714 if (event->recurrence_id)
715 cal_db_util_stmt_bind_text(stmt, index, event->recurrence_id);
718 cal_db_util_stmt_bind_text(stmt, index, event->rdate);
720 if (event->sync_data1)
721 cal_db_util_stmt_bind_text(stmt, index, event->sync_data1);
723 if (event->sync_data2)
724 cal_db_util_stmt_bind_text(stmt, index, event->sync_data2);
726 if (event->sync_data3)
727 cal_db_util_stmt_bind_text(stmt, index, event->sync_data3);
729 if (event->sync_data4)
730 cal_db_util_stmt_bind_text(stmt, index, event->sync_data4);
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);
743 * update parent event changed ver in case this event is exception mod
744 * which is original_event_id > 0
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. */
751 if (DIRTY_IN_TIME == is_dirty_in_time) {
752 cal_db_instance_discard_record(event->index);
753 cal_db_instance_publish_record(record);
756 cal_db_instance_update_exdate_del(event->index, event->exdate);
759 while ((event->uid && *event->uid) && (NULL == event->recurrence_id || '\0' == *event->recurrence_id)) {
760 /* check if exception mod has. recurrence_id */
762 list = cal_db_event_get_list_with_uid(event->uid, event->index);
765 GList *l = g_list_first(list);
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);
773 /* remove parent instance */
774 cal_db_event_apply_recurrence_id(event->index, event, recurrence_id, child_id);
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);
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);
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);
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);
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);
807 cal_db_util_notify(CAL_NOTI_TYPE_EVENT);
809 return CALENDAR_ERROR_NONE;
812 static int _cal_db_event_update_record(calendar_record_h record)
814 return __update_record(record, DIRTY_INIT);
817 static int _cal_db_event_add_exdate(int original_event_id, char* recurrence_id)
820 char query[CAL_DB_SQL_MAX_LEN];
821 sqlite3_stmt *stmt = NULL;
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);
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);
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);
841 /* add recurrence id to end of the exdate of original event. */
842 const unsigned char *temp = 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);
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;
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;
865 snprintf(exdate, len, "%s,%s", temp, recurrence_id);
866 DBG("append [%s] to already has exdate [%s]", temp, recurrence_id);
869 DBG("Failed to get exdate: event_id(%d)", original_event_id);
871 sqlite3_finalize(stmt);
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);
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);
891 cal_db_util_stmt_bind_text(stmt, index, exdate);
893 ret = cal_db_util_stmt_step(stmt);
894 sqlite3_finalize(stmt);
896 if (CALENDAR_ERROR_NONE != ret) {
897 /* LCOV_EXCL_START */
898 ERR("cal_db_util_stmt_step() Fail(%d)", ret);
903 return CALENDAR_ERROR_NONE;
906 static bool is_deleted_exception(int id)
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);
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);
923 ret = cal_db_util_stmt_step(stmt);
924 sqlite3_finalize(stmt);
926 if (CAL_SQLITE_ROW == ret) {
927 DBG("This record is already deleted, it could be exception record");
933 static int _cal_db_event_delete_record(int id)
935 int ret = CALENDAR_ERROR_NONE;
936 int calendar_book_id;
937 char query[CAL_DB_SQL_MAX_LEN] = {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;
943 DBG("delete record(id:%d)", id);
944 RETVM_IF(id < 0, CALENDAR_ERROR_INVALID_PARAMETER, "id(%d) < 0", id);
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.
952 if (true == is_deleted_exception(id)) {
953 return CALENDAR_ERROR_NONE;
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);
967 if (cal_access_control_have_write_permission(calendar_book_id) == false) {
968 /* LCOV_EXCL_START */
970 CAL_FREE(recurrence_id);
971 return CALENDAR_ERROR_PERMISSION_DENIED;
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);
979 CAL_FREE(recurrence_id);
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);
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);
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);
1002 cal_db_util_get_next_ver();
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);
1012 /* LCOV_EXCL_STOP */
1014 DBG("attendee, alarm and rrule is deleted by trigger");
1017 cal_db_instance_discard_record(id);
1018 cal_db_util_notify(CAL_NOTI_TYPE_EVENT);
1019 return CALENDAR_ERROR_NONE;
1022 static int _cal_db_event_get_all_records(int offset, int limit, calendar_list_h* out_list)
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;
1029 RETV_IF(NULL == out_list, CALENDAR_ERROR_INVALID_PARAMETER);
1031 ret = calendar_list_create(out_list);
1032 RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "calendar_list_create() Fail(%d)", ret);
1035 snprintf(offsetquery, sizeof(offsetquery), "OFFSET %d", offset);
1038 snprintf(limitquery, sizeof(limitquery), "LIMIT %d", limit);
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);
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);
1055 /* LCOV_EXCL_STOP */
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);
1068 sqlite3_finalize(stmt);
1069 CAL_FREE(query_str);
1071 /* LCOV_EXCL_STOP */
1073 _cal_db_event_get_stmt(stmt, true, record, &exception, &extended);
1076 int has_attendee = 0, has_alarm = 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);
1084 if (CALENDAR_ERROR_NONE == calendar_record_get_int(record, _calendar_event.has_alarm, &has_alarm)) {
1086 cal_db_alarm_get_records(record_id, pevent->alarm_list);
1090 _cal_db_event_exception_get_records(record_id, pevent->exception_list);
1093 cal_db_extended_get_records(record_id, CALENDAR_RECORD_TYPE_EVENT, pevent->extended_list);
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);
1101 calendar_record_destroy(record, true);
1102 sqlite3_finalize(stmt);
1103 CAL_FREE(query_str);
1105 /* LCOV_EXCL_STOP */
1109 sqlite3_finalize(stmt);
1110 CAL_FREE(query_str);
1111 return CALENDAR_ERROR_NONE;
1114 static int _cal_db_event_get_records_with_query(calendar_query_h query, int offset, int limit, calendar_list_h* out_list)
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;
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 */
1133 que = (cal_query_s *)query;
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);
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 */
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");
1156 /* LCOV_EXCL_STOP */
1160 /* make: projection */
1161 ret = cal_db_query_create_projection(query, &projection);
1163 char *query_str = NULL;
1164 /* query: 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);
1172 cal_db_append_string(&query_str, "SELECT * FROM");
1173 cal_db_append_string(&query_str, table_name);
1175 CAL_FREE(table_name);
1177 /* query: condition */
1179 cal_db_append_string(&query_str, "WHERE (");
1180 cal_db_append_string(&query_str, condition);
1181 cal_db_append_string(&query_str, ")");
1186 ret = cal_db_query_create_order(query, condition, &order);
1188 cal_db_append_string(&query_str, order);
1191 CAL_FREE(condition);
1194 char buf[CAL_STR_SHORT_LEN32] = {0};
1196 snprintf(buf, sizeof(buf), "LIMIT %d", limit);
1197 cal_db_append_string(&query_str, buf);
1200 snprintf(buf, sizeof(buf), "OFFSET %d", offset);
1201 cal_db_append_string(&query_str, buf);
1204 DBG("%s", query_str);
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);
1213 g_slist_free_full(bind_text, free);
1218 /* LCOV_EXCL_STOP */
1223 for (cursor = bind_text, i = 1; cursor; cursor = cursor->next, i++)
1224 cal_db_util_stmt_bind_text(stmt, i, cursor->data);
1227 ret = calendar_list_create(&list);
1228 if (CALENDAR_ERROR_NONE != ret) {
1229 /* LCOV_EXCL_START */
1230 ERR("calendar_list_create() Fail(%d)", ret);
1232 g_slist_free_full(bind_text, free);
1235 sqlite3_finalize(stmt);
1236 CAL_FREE(query_str);
1238 /* LCOV_EXCL_STOP */
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;
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);
1252 g_slist_free_full(bind_text, free);
1255 sqlite3_finalize(stmt);
1256 CAL_FREE(query_str);
1258 /* LCOV_EXCL_STOP */
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);
1264 cal_event_s *event = NULL;
1265 _cal_db_event_get_stmt(stmt, true, record, &exception, &extended);
1266 event = (cal_event_s*)(record);
1268 attendee = event->has_attendee;
1269 alarm = event->has_alarm;
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);
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);
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);
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);
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);
1299 g_slist_free_full(bind_text, free);
1302 sqlite3_finalize(stmt);
1303 CAL_FREE(query_str);
1305 /* LCOV_EXCL_STOP */
1310 g_slist_free_full(bind_text, free);
1314 sqlite3_finalize(stmt);
1315 CAL_FREE(query_str);
1320 return CALENDAR_ERROR_NONE;
1323 static int _cal_db_event_delete_records(int ids[], int count)
1325 int ret = CALENDAR_ERROR_NONE;
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);
1334 /* LCOV_EXCL_STOP */
1337 return CALENDAR_ERROR_NONE;
1340 static int _cal_db_event_get_count(int *out_count)
1342 RETV_IF(NULL == out_count, CALENDAR_ERROR_INVALID_PARAMETER);
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);
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);
1356 /* LCOV_EXCL_STOP */
1358 DBG("count(%d) str[%s]", count, query_str);
1359 CAL_FREE(query_str);
1362 return CALENDAR_ERROR_NONE;
1365 static int _cal_db_event_get_count_with_query(calendar_query_h query, int *out_count)
1367 cal_query_s *que = NULL;
1368 int ret = CALENDAR_ERROR_NONE;
1369 char *condition = NULL;
1372 GSList *bind_text = NULL;
1374 que = (cal_query_s *)query;
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);
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 */
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");
1397 /* LCOV_EXCL_STOP */
1401 char *query_str = NULL;
1403 cal_db_append_string(&query_str, "SELECT count(*) FROM");
1404 cal_db_append_string(&query_str, table_name);
1405 CAL_FREE(table_name);
1407 /* query: 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);
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");
1421 g_slist_free_full(bind_text, free);
1424 CAL_FREE(query_str);
1426 /* LCOV_EXCL_STOP */
1428 DBG("count(%d) str[%s]", count, query_str);
1430 if (out_count) *out_count = count;
1432 g_slist_free_full(bind_text, free);
1436 CAL_FREE(query_str);
1437 return CALENDAR_ERROR_NONE;
1440 static int _cal_db_event_replace_record(calendar_record_h record, int id)
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;
1450 int timezone_id = 0;
1453 RETV_IF(NULL == event, CALENDAR_ERROR_INVALID_PARAMETER);
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 */
1463 if (event->common.properties_flags != NULL)
1464 return _cal_db_event_update_dirty(record, -1);
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();
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)) {
1481 snprintf(query, sizeof(query), "UPDATE %s SET "
1493 "busy_status = %d, "
1494 "sensitivity = %d, "
1496 "organizer_name = ?, "
1497 "organizer_email = ?, "
1498 "meeting_status = %d, "
1499 "calendar_id = %d, "
1500 "original_event_id = %d,"
1504 "completed_time = %lld,"
1506 "dtstart_type = %d, "
1507 "dtstart_utime = %lld, "
1508 "dtstart_datetime = ?, "
1509 "dtstart_tzid = ?, "
1511 "dtend_utime = %lld, "
1512 "dtend_datetime = ?, "
1514 "last_mod = strftime('%%s', 'now'), "
1516 "recurrence_id = ?, "
1518 "has_attendee = %d, "
1520 "system_type = %d, "
1531 CAL_SCH_TYPE_EVENT,/*event->cal_type,*/
1532 event->event_status,
1534 event->timezone ? event->timezone : timezone_id,
1538 event->meeting_status,
1540 event->original_event_id,
1544 (long long int)0, /* event->completed_time, */
1545 0, /* event->progress, */
1547 event->start.type == CALENDAR_TIME_UTIME ? event->start.time.utime : 0,
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,
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);
1565 /* LCOV_EXCL_STOP */
1571 cal_db_util_stmt_bind_text(stmt, index, event->summary);
1574 if (event->description)
1575 cal_db_util_stmt_bind_text(stmt, index, event->description);
1578 if (event->location)
1579 cal_db_util_stmt_bind_text(stmt, index, event->location);
1582 if (event->categories)
1583 cal_db_util_stmt_bind_text(stmt, index, event->categories);
1587 cal_db_util_stmt_bind_text(stmt, index, event->exdate);
1591 cal_db_util_stmt_bind_text(stmt, index, event->uid);
1594 if (event->organizer_name)
1595 cal_db_util_stmt_bind_text(stmt, index, event->organizer_name);
1598 if (event->organizer_email)
1599 cal_db_util_stmt_bind_text(stmt, index, event->organizer_email);
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);
1614 if (event->start_tzid)
1615 cal_db_util_stmt_bind_text(stmt, index, event->start_tzid);
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);
1630 if (event->end_tzid)
1631 cal_db_util_stmt_bind_text(stmt, index, event->end_tzid);
1634 if (event->recurrence_id)
1635 cal_db_util_stmt_bind_text(stmt, index, event->recurrence_id);
1638 cal_db_util_stmt_bind_text(stmt, index, event->rdate);
1640 if (event->sync_data1)
1641 cal_db_util_stmt_bind_text(stmt, index, event->sync_data1);
1643 if (event->sync_data2)
1644 cal_db_util_stmt_bind_text(stmt, index, event->sync_data2);
1646 if (event->sync_data3)
1647 cal_db_util_stmt_bind_text(stmt, index, event->sync_data3);
1649 if (event->sync_data4)
1650 cal_db_util_stmt_bind_text(stmt, index, event->sync_data4);
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);
1659 /* LCOV_EXCL_STOP */
1663 * update parent event changed ver in case this event is exception mod
1664 * which is original_event_id > 0
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);
1671 cal_db_instance_discard_record(id);
1672 cal_db_instance_publish_record(record);
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);
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);
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);
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);
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);
1699 cal_db_util_notify(CAL_NOTI_TYPE_EVENT);
1701 return CALENDAR_ERROR_NONE;
1704 static void _cal_db_event_get_stmt(sqlite3_stmt *stmt, bool is_view_table, calendar_record_h record,
1705 int *exception, int *extended)
1707 cal_event_s *event = NULL;
1708 const unsigned char *temp;
1711 event = (cal_event_s*)(record);
1713 event->index = sqlite3_column_int(stmt, count++);
1714 sqlite3_column_int(stmt, count++);
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);
1721 temp = sqlite3_column_text(stmt, count++);
1722 event->location = cal_strdup((const char*)temp);
1724 temp = sqlite3_column_text(stmt, count++);
1725 event->categories = cal_strdup((const char*)temp);
1727 temp = sqlite3_column_text(stmt, count++);
1728 event->exdate = cal_strdup((const char*)temp);
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++);
1737 temp = sqlite3_column_text(stmt, count++);
1738 event->uid = cal_strdup((const char*)temp);
1740 temp = sqlite3_column_text(stmt, count++);
1741 event->organizer_name = cal_strdup((const char*)temp);
1743 temp = sqlite3_column_text(stmt, count++);
1744 event->organizer_email = cal_strdup((const char*)temp);
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++);
1754 count++; /* completed_time */
1755 count++; /* progress */
1756 count++; /* changed_ver */
1757 count++; /* created_ver */
1759 event->is_deleted = sqlite3_column_int(stmt, count++);
1760 event->start.type = sqlite3_column_int(stmt, count++);
1762 if (event->start.type == CALENDAR_TIME_UTIME) {
1763 event->start.time.utime = sqlite3_column_int64(stmt, count++);
1764 count++; /* dtstart_datetime */
1766 count++; /* dtstart_utime */
1767 temp = sqlite3_column_text(stmt, count++);
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));
1776 temp = sqlite3_column_text(stmt, count++);
1777 event->start_tzid = cal_strdup((const char*)temp);
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 */
1784 count++; /* dtend_utime */
1785 temp = sqlite3_column_text(stmt, count++);
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));
1793 temp = sqlite3_column_text(stmt, count++);
1794 event->end_tzid = cal_strdup((const char*)temp);
1796 event->last_mod = sqlite3_column_int64(stmt, count++);
1797 sqlite3_column_int(stmt, count++);
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);
1818 *exception = sqlite3_column_int(stmt, count++);
1822 *extended = sqlite3_column_int(stmt, count++);
1824 event->freq = sqlite3_column_int(stmt, count++);
1825 event->is_allday = sqlite3_column_int(stmt, count++);
1827 if (is_view_table == true) {
1828 if (event->freq <= 0)
1831 event->range_type = sqlite3_column_int(stmt, count++);
1832 event->until.type = sqlite3_column_int(stmt, count++);
1834 switch (event->until.type) {
1835 case CALENDAR_TIME_UTIME:
1836 event->until.time.utime = sqlite3_column_int64(stmt, count++);
1837 count++; /* datetime */
1840 case CALENDAR_TIME_LOCALTIME:
1841 count++; /* utime */
1842 temp = sqlite3_column_text(stmt, count++);
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));
1852 event->count = sqlite3_column_int(stmt, count++);
1853 event->interval = sqlite3_column_int(stmt, count++);
1855 temp = sqlite3_column_text(stmt, count++);
1856 event->bysecond = cal_strdup((const char*)temp);
1858 temp = sqlite3_column_text(stmt, count++);
1859 event->byminute = cal_strdup((const char*)temp);
1861 temp = sqlite3_column_text(stmt, count++);
1862 event->byhour = cal_strdup((const char*)temp);
1864 temp = sqlite3_column_text(stmt, count++);
1865 event->byday = cal_strdup((const char*)temp);
1867 temp = sqlite3_column_text(stmt, count++);
1868 event->bymonthday = cal_strdup((const char*)temp);
1870 temp = sqlite3_column_text(stmt, count++);
1871 event->byyearday = cal_strdup((const char*)temp);
1873 temp = sqlite3_column_text(stmt, count++);
1874 event->byweekno = cal_strdup((const char*)temp);
1876 temp = sqlite3_column_text(stmt, count++);
1877 event->bymonth = cal_strdup((const char*)temp);
1879 temp = sqlite3_column_text(stmt, count++);
1880 event->bysetpos = cal_strdup((const char*)temp);
1882 event->wkst = sqlite3_column_int(stmt, count++);
1884 sqlite3_column_int(stmt, count++); /* calendar deleted */
1889 static void _cal_db_event_get_property_stmt(sqlite3_stmt *stmt,
1890 unsigned int property, int *stmt_count, calendar_record_h record)
1892 cal_event_s *event = NULL;
1893 const unsigned char *temp;
1895 event = (cal_event_s*)(record);
1898 case CAL_PROPERTY_EVENT_ID:
1899 event->index = sqlite3_column_int(stmt, *stmt_count);
1901 case CAL_PROPERTY_EVENT_BOOK_ID:
1902 event->calendar_id = sqlite3_column_int(stmt, *stmt_count);
1904 case CAL_PROPERTY_EVENT_SUMMARY:
1905 temp = sqlite3_column_text(stmt, *stmt_count);
1906 event->summary = cal_strdup((const char*)temp);
1908 case CAL_PROPERTY_EVENT_DESCRIPTION:
1909 temp = sqlite3_column_text(stmt, *stmt_count);
1910 event->description = cal_strdup((const char*)temp);
1912 case CAL_PROPERTY_EVENT_LOCATION:
1913 temp = sqlite3_column_text(stmt, *stmt_count);
1914 event->location = cal_strdup((const char*)temp);
1916 case CAL_PROPERTY_EVENT_CATEGORIES:
1917 temp = sqlite3_column_text(stmt, *stmt_count);
1918 event->categories = cal_strdup((const char*)temp);
1920 case CAL_PROPERTY_EVENT_EXDATE:
1921 temp = sqlite3_column_text(stmt, *stmt_count);
1922 event->exdate = cal_strdup((const char*)temp);
1924 case CAL_PROPERTY_EVENT_EVENT_STATUS:
1925 event->event_status = sqlite3_column_int(stmt, *stmt_count);
1927 case CAL_PROPERTY_EVENT_PRIORITY:
1928 event->priority = sqlite3_column_int(stmt, *stmt_count);
1930 case CAL_PROPERTY_EVENT_TIMEZONE:
1931 event->timezone = sqlite3_column_int(stmt, *stmt_count);
1933 case CAL_PROPERTY_EVENT_CONTACT_ID:
1934 event->contact_id = sqlite3_column_int(stmt, *stmt_count);
1936 case CAL_PROPERTY_EVENT_BUSY_STATUS:
1937 event->busy_status = sqlite3_column_int(stmt, *stmt_count);
1939 case CAL_PROPERTY_EVENT_SENSITIVITY:
1940 event->sensitivity = sqlite3_column_int(stmt, *stmt_count);
1942 case CAL_PROPERTY_EVENT_UID:
1943 temp = sqlite3_column_text(stmt, *stmt_count);
1944 event->uid = cal_strdup((const char*)temp);
1946 case CAL_PROPERTY_EVENT_ORGANIZER_NAME:
1947 temp = sqlite3_column_text(stmt, *stmt_count);
1948 event->organizer_name = cal_strdup((const char*)temp);
1950 case CAL_PROPERTY_EVENT_ORGANIZER_EMAIL:
1951 temp = sqlite3_column_text(stmt, *stmt_count);
1952 event->organizer_email = cal_strdup((const char*)temp);
1954 case CAL_PROPERTY_EVENT_MEETING_STATUS:
1955 event->meeting_status = sqlite3_column_int(stmt, *stmt_count);
1957 case CAL_PROPERTY_EVENT_ORIGINAL_EVENT_ID:
1958 event->original_event_id = sqlite3_column_int(stmt, *stmt_count);
1960 case CAL_PROPERTY_EVENT_LATITUDE:
1961 event->latitude = sqlite3_column_double(stmt, *stmt_count);
1963 case CAL_PROPERTY_EVENT_LONGITUDE:
1964 event->longitude = sqlite3_column_double(stmt, *stmt_count);
1966 case CAL_PROPERTY_EVENT_EMAIL_ID:
1967 event->email_id = sqlite3_column_int(stmt, *stmt_count);
1969 case CAL_PROPERTY_EVENT_CREATED_TIME:
1970 event->created_time = sqlite3_column_int64(stmt, *stmt_count);
1972 case CAL_PROPERTY_EVENT_LAST_MODIFIED_TIME:
1973 event->last_mod = sqlite3_column_int64(stmt, *stmt_count);
1975 case CAL_PROPERTY_EVENT_IS_DELETED:
1976 event->is_deleted = sqlite3_column_int(stmt, *stmt_count);
1978 case CAL_PROPERTY_EVENT_FREQ:
1979 event->freq = sqlite3_column_int(stmt, *stmt_count);
1981 case CAL_PROPERTY_EVENT_RANGE_TYPE:
1982 event->range_type = sqlite3_column_int(stmt, *stmt_count);
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 */
1991 *stmt_count = *stmt_count+1;
1992 *stmt_count = *stmt_count+1;
1993 temp = sqlite3_column_text(stmt, *stmt_count);
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));
2002 case CAL_PROPERTY_EVENT_COUNT:
2003 event->count = sqlite3_column_int(stmt, *stmt_count);
2005 case CAL_PROPERTY_EVENT_INTERVAL:
2006 event->interval = sqlite3_column_int(stmt, *stmt_count);
2008 case CAL_PROPERTY_EVENT_BYSECOND:
2009 temp = sqlite3_column_text(stmt, *stmt_count);
2010 event->bysecond = cal_strdup((const char*)temp);
2012 case CAL_PROPERTY_EVENT_BYMINUTE:
2013 temp = sqlite3_column_text(stmt, *stmt_count);
2014 event->byminute = cal_strdup((const char*)temp);
2016 case CAL_PROPERTY_EVENT_BYHOUR:
2017 temp = sqlite3_column_text(stmt, *stmt_count);
2018 event->byhour = cal_strdup((const char*)temp);
2020 case CAL_PROPERTY_EVENT_BYDAY:
2021 temp = sqlite3_column_text(stmt, *stmt_count);
2022 event->byday = cal_strdup((const char*)temp);
2024 case CAL_PROPERTY_EVENT_BYMONTHDAY:
2025 temp = sqlite3_column_text(stmt, *stmt_count);
2026 event->bymonthday = cal_strdup((const char*)temp);
2028 case CAL_PROPERTY_EVENT_BYYEARDAY:
2029 temp = sqlite3_column_text(stmt, *stmt_count);
2030 event->byyearday = cal_strdup((const char*)temp);
2032 case CAL_PROPERTY_EVENT_BYWEEKNO:
2033 temp = sqlite3_column_text(stmt, *stmt_count);
2034 event->byweekno = cal_strdup((const char*)temp);
2036 case CAL_PROPERTY_EVENT_BYMONTH:
2037 temp = sqlite3_column_text(stmt, *stmt_count);
2038 event->bymonth = cal_strdup((const char*)temp);
2040 case CAL_PROPERTY_EVENT_BYSETPOS:
2041 temp = sqlite3_column_text(stmt, *stmt_count);
2042 event->bysetpos = cal_strdup((const char*)temp);
2044 case CAL_PROPERTY_EVENT_WKST:
2045 event->wkst = sqlite3_column_int(stmt, *stmt_count);
2047 case CAL_PROPERTY_EVENT_RECURRENCE_ID:
2048 temp = sqlite3_column_text(stmt, *stmt_count);
2049 event->recurrence_id = cal_strdup((const char*)temp);
2051 case CAL_PROPERTY_EVENT_RDATE:
2052 temp = sqlite3_column_text(stmt, *stmt_count);
2053 event->rdate = cal_strdup((const char*)temp);
2055 case CAL_PROPERTY_EVENT_HAS_ATTENDEE:
2056 event->has_attendee = sqlite3_column_int(stmt, *stmt_count);
2058 case CAL_PROPERTY_EVENT_HAS_ALARM:
2059 event->has_alarm = sqlite3_column_int(stmt, *stmt_count);
2061 case CAL_PROPERTY_EVENT_SYNC_DATA1:
2062 temp = sqlite3_column_text(stmt, *stmt_count);
2063 event->sync_data1 = cal_strdup((const char*)temp);
2065 case CAL_PROPERTY_EVENT_SYNC_DATA2:
2066 temp = sqlite3_column_text(stmt, *stmt_count);
2067 event->sync_data2 = cal_strdup((const char*)temp);
2069 case CAL_PROPERTY_EVENT_SYNC_DATA3:
2070 temp = sqlite3_column_text(stmt, *stmt_count);
2071 event->sync_data3 = cal_strdup((const char*)temp);
2073 case CAL_PROPERTY_EVENT_SYNC_DATA4:
2074 temp = sqlite3_column_text(stmt, *stmt_count);
2075 event->sync_data4 = cal_strdup((const char*)temp);
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 */
2084 *stmt_count = *stmt_count+1;
2085 *stmt_count = *stmt_count+1;
2086 temp = sqlite3_column_text(stmt, *stmt_count);
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));
2095 case CAL_PROPERTY_EVENT_START_TZID:
2096 temp = sqlite3_column_text(stmt, *stmt_count);
2097 event->start_tzid = cal_strdup((const char*)temp);
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 */
2106 *stmt_count = *stmt_count+1; /* dtend_utime */
2107 *stmt_count = *stmt_count+1;
2108 temp = sqlite3_column_text(stmt, *stmt_count);
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));
2117 case CAL_PROPERTY_EVENT_END_TZID:
2118 temp = sqlite3_column_text(stmt, *stmt_count);
2119 event->end_tzid = cal_strdup((const char*)temp);
2121 case CAL_PROPERTY_EVENT_CALENDAR_SYSTEM_TYPE:
2122 event->system_type = sqlite3_column_int(stmt, *stmt_count);
2124 case CAL_PROPERTY_EVENT_IS_ALLDAY:
2125 event->is_allday = sqlite3_column_int(stmt, *stmt_count);
2128 /* LCOV_EXCL_START */
2129 ERR("invalid (0x%x)", property);
2131 /* LCOV_EXCL_STOP */
2134 *stmt_count = *stmt_count+1;
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)
2144 for (i = 0; i < projection_count; i++)
2145 _cal_db_event_get_property_stmt(stmt, projection[i], &stmt_count, record);
2148 static bool _cal_db_event_check_calendar_book_type(calendar_record_h record)
2152 cal_event_s *event = (cal_event_s *)record;
2153 char query[CAL_DB_SQL_MAX_LEN];
2154 sqlite3_stmt *stmt = NULL;
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);
2164 /* LCOV_EXCL_STOP */
2167 if (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
2168 store_type = sqlite3_column_int(stmt, 0);
2170 /* LCOV_EXCL_START */
2171 ERR("Failed to get calendar: calendar_id(%d)", event->calendar_id);
2172 sqlite3_finalize(stmt);
2175 /* LCOV_EXCL_STOP */
2177 sqlite3_finalize(stmt);
2179 switch (store_type) {
2180 case CALENDAR_BOOK_TYPE_NONE:
2181 case CALENDAR_BOOK_TYPE_EVENT:
2184 case CALENDAR_BOOK_TYPE_TODO:
2192 static int _cal_db_event_delete_exception(int *exception_ids, int exception_len)
2195 char query[CAL_DB_SQL_MAX_LEN] = {0};
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);
2206 /* LCOV_EXCL_STOP */
2209 return CALENDAR_ERROR_NONE;
2212 static int _cal_db_event_update_dirty(calendar_record_h record, int is_dirty_in_time)
2215 int ret = CALENDAR_ERROR_NONE;
2216 calendar_record_h original_record = NULL;
2218 ret = calendar_record_get_int(record, _calendar_event.id, &event_id);
2219 RETV_IF(CALENDAR_ERROR_NONE != ret, ret);
2221 DBG("id=%d", event_id);
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);
2226 cal_record_s *_record = NULL;
2227 const cal_property_info_s* property_info = NULL;
2228 int property_info_count = 0;
2231 _record = (cal_record_s *)record;
2233 property_info = cal_view_get_property_info(_record->view_uri, &property_info_count);
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))
2239 if (property_info[i].property_id == CAL_PROPERTY_EVENT_EXDATE) {
2240 int calendar_id = 0;
2242 calendar_book_sync_event_type_e sync_event_type = CALENDAR_BOOK_SYNC_EVENT_FOR_ME;
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)
2248 ret = calendar_record_get_int(original_record, CAL_PROPERTY_EVENT_BOOK_ID, &calendar_id);
2249 if (CALENDAR_ERROR_NONE != ret)
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);
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)
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);
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);
2278 ret = cal_record_set_str(original_record, property_info[i].property_id, record_exdate);
2279 if (CALENDAR_ERROR_NONE != ret)
2281 } else if (CAL_PROPERTY_CHECK_DATA_TYPE(property_info[i].property_id, CAL_PROPERTY_DATA_TYPE_INT) == true) {
2283 ret = calendar_record_get_int(record, property_info[i].property_id, &tmp);
2284 if (CALENDAR_ERROR_NONE != ret)
2286 ret = cal_record_set_int(original_record, property_info[i].property_id, tmp);
2287 if (CALENDAR_ERROR_NONE != ret)
2289 } else if (CAL_PROPERTY_CHECK_DATA_TYPE(property_info[i].property_id, CAL_PROPERTY_DATA_TYPE_STR) == true) {
2291 ret = calendar_record_get_str_p(record, property_info[i].property_id, &tmp);
2292 if (CALENDAR_ERROR_NONE != ret)
2294 ret = cal_record_set_str(original_record, property_info[i].property_id, tmp);
2295 if (CALENDAR_ERROR_NONE != ret)
2297 } else if (CAL_PROPERTY_CHECK_DATA_TYPE(property_info[i].property_id, CAL_PROPERTY_DATA_TYPE_DOUBLE) == true) {
2299 ret = calendar_record_get_double(record, property_info[i].property_id, &tmp);
2300 if (CALENDAR_ERROR_NONE != ret)
2302 ret = cal_record_set_double(original_record, property_info[i].property_id, tmp);
2303 if (CALENDAR_ERROR_NONE != ret)
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)
2310 ret = cal_record_set_lli(original_record, property_info[i].property_id, tmp);
2311 if (CALENDAR_ERROR_NONE != ret)
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)
2318 ret = cal_record_set_caltime(original_record, property_info[i].property_id, tmp);
2319 if (CALENDAR_ERROR_NONE != ret)
2324 cal_event_s *tmp = (cal_event_s *)original_record;
2325 cal_event_s *tmp_src = (cal_event_s *)record;
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);
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);
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);
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);
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);
2350 static int _cal_db_event_exception_get_records(int original_id, cal_list_s *list)
2353 char query[CAL_DB_SQL_MAX_LEN] = {0};
2354 sqlite3_stmt *stmt = NULL;
2356 RETVM_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER,
2357 "Invalid parameter: list is NULL");
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 ",
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);
2371 /* LCOV_EXCL_STOP */
2374 calendar_record_h record = NULL;
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);
2385 /* LCOV_EXCL_STOP */
2388 _cal_db_event_get_stmt(stmt, false, record, &exception, &extended);
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);
2400 if (event->has_alarm == 1)
2401 cal_db_alarm_get_records(event->index, event->alarm_list);
2403 if (event->has_attendee == 1)
2404 cal_db_attendee_get_records(event->index, event->attendee_list);
2407 _cal_db_event_exception_get_records(event->index, event->exception_list);
2410 cal_db_extended_get_records(event->index, CALENDAR_RECORD_TYPE_EVENT, event->extended_list);
2412 event->has_alarm = 0;
2413 if (event->alarm_list && 0 < event->alarm_list->count)
2414 event->has_alarm = 1;
2416 event->has_attendee = 0;
2417 if (event->attendee_list && 0 < event->attendee_list->count)
2418 event->has_attendee = 1;
2420 calendar_list_add((calendar_list_h)list, record);
2422 sqlite3_finalize(stmt);
2423 return CALENDAR_ERROR_NONE;
2426 static int _cal_db_event_exception_delete_with_id(int original_id)
2429 char query[CAL_DB_SQL_MAX_LEN] = {0};
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);
2440 /* LCOV_EXCL_STOP */
2443 return CALENDAR_ERROR_NONE;
2446 static int _cal_db_event_exception_get_ids(int original_id, GList **out_list)
2449 char query[CAL_DB_SQL_MAX_LEN] = {0};
2450 sqlite3_stmt *stmt = NULL;
2453 RETV_IF(NULL == out_list, CALENDAR_ERROR_INVALID_PARAMETER);
2455 snprintf(query, sizeof(query), "SELECT id FROM %s WHERE original_event_id = %d AND is_deleted = 0 ",
2456 CAL_TABLE_SCHEDULE, original_id);
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);
2464 /* LCOV_EXCL_STOP */
2467 while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
2469 id = sqlite3_column_int(stmt, 0);
2470 list = g_list_append(list, GINT_TO_POINTER(id));
2473 sqlite3_finalize(stmt);
2476 return CALENDAR_ERROR_NONE;
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)
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;
2487 _cal_db_event_exception_get_ids(original_id, &id_list);
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;
2502 _record = (cal_record_s *)exception;
2504 property_info = cal_view_get_property_info(_record->view_uri, &property_info_count);
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)) {
2515 if (bchanged == true || DIRTY_IN_TIME == is_dirty_in_time) {
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);
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);
2529 id_list = g_list_remove(id_list, GINT_TO_POINTER(exception_id));
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);
2538 g_list_free(id_list);
2540 /* LCOV_EXCL_STOP */
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);
2547 g_list_free(id_list);
2549 /* LCOV_EXCL_STOP */
2552 calendar_list_next(exception_list);
2557 GList * tmp_list = g_list_first(id_list);
2559 int tmp = GPOINTER_TO_INT(tmp_list->data);
2560 char query[CAL_DB_SQL_MAX_LEN] = {0};
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 */
2570 tmp_list = g_list_next(tmp_list);
2572 g_list_free(id_list);
2574 return CALENDAR_ERROR_NONE;
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)
2581 int ret = CALENDAR_ERROR_NONE;
2582 char query[CAL_DB_SQL_MAX_LEN] = {0};
2583 sqlite3_stmt *stmt = NULL;
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);
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);
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);
2600 /* LCOV_EXCL_STOP */
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 */
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));
2618 sqlite3_finalize(stmt);
2620 return CALENDAR_ERROR_NONE;
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)
2626 int ret = CALENDAR_ERROR_NONE;
2627 gchar **patterns1 = NULL;
2628 gchar **patterns2 = NULL;
2629 int len1 = 0, len2 = 0, i = 0, j = 0;
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);
2636 if (original_exdate && 0 < strlen(original_exdate)) {
2637 patterns2 = g_strsplit_set(original_exdate, " ,", -1);
2638 len2 = g_strv_length(patterns2);
2641 int *ids = calloc(len1, sizeof(int));
2642 RETVM_IF(NULL == ids, CALENDAR_ERROR_OUT_OF_MEMORY, "calloc() Fail");
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");
2651 for (j = 0; j < len2; j++) {
2652 if (NULL == patterns2[j]) {
2653 WARN("original exdate is NULL");
2656 if (CAL_STRING_EQUAL == strcmp(patterns1[i], patterns2[j])) {
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);
2672 snprintf(datetime, sizeof(datetime),
2673 "%s", patterns1[i]);
2676 snprintf(query, sizeof(query),
2679 "created_ver, changed_ver, "
2681 "original_event_id, "
2686 "dtstart_datetime, "
2703 "%lld+(dtend_utime-dtstart_utime), "
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 */
2727 int event_id = cal_db_util_last_insert_id();
2728 DBG("last id(%d)", event_id);
2729 ids[exception_count] = event_id;
2734 *exception_ids = ids;
2739 *exception_len = exception_count;
2741 g_strfreev(patterns1);
2742 g_strfreev(patterns2);