[TSAM-8589] clean exception list when child record is removed 43/90943/7 accepted/tizen/3.0/ivi/20161028.134048 accepted/tizen/3.0/mobile/20161028.133205 accepted/tizen/common/20161012.154614 accepted/tizen/ivi/20161013.000712 accepted/tizen/mobile/20161013.000641 submit/tizen/20161012.062243 submit/tizen_3.0/20161028.062323 submit/tizen_3.0/20161028.082423 submit/tizen_3.0_common/20161104.104000
authorJeesun Kim <iamjs.kim@samsung.com>
Wed, 5 Oct 2016 05:49:39 +0000 (14:49 +0900)
committerJeesun Kim <iamjs.kim@samsung.com>
Mon, 10 Oct 2016 05:30:08 +0000 (14:30 +0900)
Change-Id: I922544969c9e855160d272a327f5f292d4eaf84b

packaging/calendar-service.spec
server/db/cal_db_plugin_event.c

index 237742e..629e4a4 100644 (file)
@@ -1,6 +1,6 @@
 Name:       calendar-service
 Summary:    DB library for calendar
-Version:    0.1.171
+Version:    0.1.172
 Release:    1
 Group:      System/Libraries
 License:    Apache-2.0
index 533af83..009e977 100644 (file)
@@ -59,16 +59,14 @@ static void _cal_db_event_get_stmt(sqlite3_stmt *stmt, bool is_view_table, calen
 static void _cal_db_event_get_property_stmt(sqlite3_stmt *stmt, unsigned int property, int *stmt_count, calendar_record_h record);
 static void _cal_db_event_get_projection_stmt(sqlite3_stmt *stmt, const unsigned int *projection, const int projection_count, calendar_record_h record);
 static int _cal_db_event_exception_get_records(int original_id, cal_list_s *list);
-static int _cal_db_event_exception_delete_with_id(int original_id);
 static int _cal_db_event_exception_get_ids(int original_id, GList **out_list);
 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);
 static int _cal_db_event_get_deleted_data(int id, int* calendar_book_id, int* created_ver, int* original_event_id, char** recurrence_id);
-static int _cal_db_event_exdate_insert_utime(int event_id, const char* original_exdate, const char* exdate, int **exception_id, int *exception_len);
+static int _cal_db_event_exdate_insert_utime(int event_id, const char* original_exdate, const char* exdate, int **exception_id, int *exception_len, bool *needs_update);
 static bool _cal_db_event_check_calendar_book_type(calendar_record_h record);
 
 static void __check_list(calendar_list_h l)
 {
-       DBG("---------------------");
        calendar_list_first(l);
        int count = 0;
        calendar_list_get_count(l, &count);
@@ -302,7 +300,7 @@ time_t __get_time_diff(char *old_tzid, calendar_time_s *old, char *new_tzid, cal
                }
                break;
        }
-       DBG("-------------time diff(%ld)", diff);
+       DBG("time diff(%ld)", diff);
        return diff;
 }
 
@@ -476,6 +474,29 @@ static int __update_recurrence_id(calendar_record_h exception, int old_type, int
        return CALENDAR_ERROR_NONE;
 }
 
+static void _cal_db_exception_delete_with_id(int original_event_id)
+{
+       int ret = 0;
+       sqlite3_stmt *stmt = NULL;
+       char query[CAL_DB_SQL_MAX_LEN];
+       snprintf(query, sizeof(query), "SELECT id FROM "CAL_TABLE_SCHEDULE" "
+                       "WHERE original_event_id = %d ", original_event_id);
+       ret = cal_db_util_query_prepare(query, &stmt);
+       if (CALENDAR_ERROR_NONE != ret) {
+               /* LCOV_EXCL_START */
+               ERR("cal_db_util_query_prepare() Fail(%d)", ret);
+               SECURE("query[%s]", query);
+               return;
+               /* LCOV_EXCL_STOP */
+       }
+
+       while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
+               int num = sqlite3_column_int(stmt, 0);
+               _cal_db_event_delete_record(num);
+       }
+       sqlite3_finalize(stmt);
+}
+
 static int __update_record(calendar_record_h record, int is_dirty_in_time)
 {
        int ret = 0;
@@ -782,6 +803,7 @@ static int __update_record(calendar_record_h record, int is_dirty_in_time)
 
        cal_db_alarm_delete_with_id(event->index);
        cal_db_attendee_delete_with_id(event->index);
+       _cal_db_exception_delete_with_id(event->index);
        cal_db_extended_delete_with_id(event->index, CALENDAR_RECORD_TYPE_EVENT);
 
        if (event->alarm_list && 0 < event->alarm_list->count) {
@@ -795,7 +817,8 @@ static int __update_record(calendar_record_h record, int is_dirty_in_time)
        }
 
        if (event->exception_list && 0 < event->exception_list->count) {
-               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);
+               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);
                WARN_IF(CALENDAR_ERROR_NONE != ret, "_cal_db_event_exception_update() Fail(%d)", ret);
        }
 
@@ -946,7 +969,7 @@ static int _cal_db_event_delete_record(int id)
 
        /*
         * if parent is deleted first, exception record is failed to be deleted.
-        * Becase exception record is deleted first, ret has SQLITE_DONE
+        * Because exception record is deleted first, ret has SQLITE_DONE
         * Trigger in schema deletes exception first, before deleting parent.
         */
        if (true == is_deleted_exception(id)) {
@@ -1673,7 +1696,7 @@ static int _cal_db_event_replace_record(calendar_record_h record, int id)
 
        cal_db_alarm_delete_with_id(id);
        cal_db_attendee_delete_with_id(id);
-       _cal_db_event_exception_delete_with_id(id);
+       _cal_db_exception_delete_with_id(id);
        cal_db_extended_delete_with_id(id, CALENDAR_RECORD_TYPE_EVENT);
 
        if (event->alarm_list && 0 < event->alarm_list->count) {
@@ -2260,24 +2283,28 @@ static int _cal_db_event_update_dirty(calendar_record_h record, int is_dirty_in_
                        ret = calendar_record_get_str_p(original_record, property_info[i].property_id, &original_exdate);
                        if (CALENDAR_ERROR_NONE != ret)
                                continue;
+
+                       bool needs_update = false;
                        if (sync_event_type == CALENDAR_BOOK_SYNC_EVENT_FOR_EVERY_AND_REMAIN) {
                                ret = _cal_db_event_exdate_insert_utime(event_id, original_exdate,
-                                               record_exdate, NULL, NULL);
+                                               record_exdate, NULL, NULL, &needs_update);
                                WARN_IF(CALENDAR_ERROR_NONE != ret, "%s->%s", original_exdate, record_exdate);
 
                        } else {
                                int *exception_ids = NULL;
                                int exception_len = 0;
                                ret = _cal_db_event_exdate_insert_utime(event_id, original_exdate,
-                                               record_exdate, &exception_ids, &exception_len);
+                                               record_exdate, &exception_ids, &exception_len, &needs_update);
                                WARN_IF(CALENDAR_ERROR_NONE != ret, "%s->%s", original_exdate, record_exdate);
                                ret = _cal_db_event_delete_exception(exception_ids, exception_len);
                                WARN_IF(CALENDAR_ERROR_NONE != ret, "_cal_db_event_delete_record() Fail");
                                free(exception_ids);
                        }
-                       ret = cal_record_set_str(original_record, property_info[i].property_id, record_exdate);
-                       if (CALENDAR_ERROR_NONE != ret)
-                               continue;
+                       if (needs_update) {
+                               ret = cal_record_set_str(original_record, property_info[i].property_id, record_exdate);
+                               if (CALENDAR_ERROR_NONE != ret)
+                                       continue;
+                       }
                } else if (CAL_PROPERTY_CHECK_DATA_TYPE(property_info[i].property_id, CAL_PROPERTY_DATA_TYPE_INT) == true) {
                        int tmp = 0;
                        ret = calendar_record_get_int(record, property_info[i].property_id, &tmp);
@@ -2423,26 +2450,6 @@ static int _cal_db_event_exception_get_records(int original_id, cal_list_s *list
        return CALENDAR_ERROR_NONE;
 }
 
-static int _cal_db_event_exception_delete_with_id(int original_id)
-{
-       int ret = 0;
-       char query[CAL_DB_SQL_MAX_LEN] = {0};
-
-       DBG("delete exception mod with original event id(%d)", original_id);
-       snprintf(query, sizeof(query), "DELETE FROM %s WHERE original_event_id=%d ",
-                       CAL_TABLE_SCHEDULE, original_id);
-       ret = cal_db_util_query_exec(query);
-       if (CALENDAR_ERROR_NONE != ret) {
-               /* LCOV_EXCL_START */
-               ERR("cal_db_util_query_exec() Fail(%d)", ret);
-               SECURE("[%s]", query);
-               return ret;
-               /* LCOV_EXCL_STOP */
-       }
-
-       return CALENDAR_ERROR_NONE;
-}
-
 static int _cal_db_event_exception_get_ids(int original_id, GList **out_list)
 {
        int ret = 0;
@@ -2621,102 +2628,73 @@ static int _cal_db_event_get_deleted_data(int id, int* book_id, int* created_ver
 }
 
 static int _cal_db_event_exdate_insert_utime(int event_id, const char* original_exdate,
-               const char* exdate, int **exception_ids, int *exception_len)
+               const char* inserted_exdate, int **exception_ids, int *exception_len, bool *needs_update)
 {
        int ret = CALENDAR_ERROR_NONE;
-       gchar **patterns1 = NULL;
-       gchar **patterns2 = NULL;
-       int len1 = 0, len2 = 0, i = 0, j = 0;
+       gchar **t_inserted = NULL;
+       gchar **t_original = NULL;
+       int len_inserted = 0;
+       int len_original = 0;
 
-       int input_ver = cal_db_util_get_next_ver();
-       if (exdate != NULL && 0 < strlen(exdate)) {
-               patterns1 = g_strsplit_set(exdate, " ,", -1);
-               len1 = g_strv_length(patterns1);
+       if (NULL != inserted_exdate && '\0' != *inserted_exdate) {
+               t_inserted = g_strsplit_set(inserted_exdate, " ,", -1);
+               len_inserted = g_strv_length(t_inserted);
        }
-       if (original_exdate && 0 < strlen(original_exdate)) {
-               patterns2 = g_strsplit_set(original_exdate, " ,", -1);
-               len2 = g_strv_length(patterns2);
+       if (NULL != original_exdate && '\0' != *original_exdate) {
+               t_original = g_strsplit_set(original_exdate, " ,", -1);
+               len_original = g_strv_length(t_original);
        }
 
-       int *ids = calloc(len1, sizeof(int));
-       RETVM_IF(NULL == ids, CALENDAR_ERROR_OUT_OF_MEMORY, "calloc() Fail");
+       int *ids = calloc(len_inserted, sizeof(int));
+       if (NULL == ids) {
+               ERR("calloc() Fail");
+               g_strfreev(t_inserted);
+               g_strfreev(t_original);
+               return CALENDAR_ERROR_OUT_OF_MEMORY;
+       }
 
        int exception_count = 0;
-       for (i = 0; i < len1; i++) {
-               if (NULL == patterns1[i]) {
-                       WARN("exdate is NULL, so check next");
+       int input_ver = cal_db_util_get_next_ver();
+       int i = 0, j = 0;
+       for (i = 0; i < len_inserted; i++) {
+               if ('\0' == *(t_inserted[i]))
                        continue;
-               }
+
                bool bFind = false;
-               for (j = 0; j < len2; j++) {
-                       if (NULL == patterns2[j]) {
-                               WARN("original exdate is NULL");
+               for (j = 0; j < len_original; j++) {
+                       if ('\0' == *(t_original[j]))
                                continue;
-                       }
-                       if (CAL_STRING_EQUAL == strcmp(patterns1[i], patterns2[j])) {
+
+                       if (CAL_STRING_EQUAL == strcmp(t_inserted[i], t_original[j])) {
                                bFind = true;
                                break;
                        }
                }
+
                if (bFind == false) {
                        char query[CAL_DB_SQL_MAX_LEN] = {0};
                        long long int start_utime = 0;
                        char datetime[16] = {0};
-                       if (strlen("YYYYMMDD") < strlen(patterns1[i])) {
-                               int y, mon, d, h, min, s;
-                               sscanf(patterns1[i], CAL_DATETIME_FORMAT_YYYYMMDDTHHMMSSZ,
-                                               &y, &mon, &d, &h, &min, &s);
-                               start_utime = cal_time_convert_itol(NULL, y, mon, d, h, min, s);
+                       if (strlen("YYYYMMDD") < strlen(t_inserted[i])) {
+                               int y, m, d, h, n, s;
+                               sscanf(t_inserted[i], CAL_DATETIME_FORMAT_YYYYMMDDTHHMMSSZ, &y, &m, &d, &h, &n, &s);
+                               start_utime = cal_time_convert_itol(NULL, y, m, d, h, n, s);
 
                        } else {
-                               snprintf(datetime, sizeof(datetime),
-                                               "%s", patterns1[i]);
-
+                               snprintf(datetime, sizeof(datetime), "%s", t_inserted[i]);
                        }
-                       snprintf(query, sizeof(query),
-                                       "INSERT INTO %s ("
-                                       "type, "
-                                       "created_ver, changed_ver, "
-                                       "calendar_id, "
-                                       "original_event_id, "
-                                       "recurrence_id, "
-                                       "is_deleted, "
-                                       "dtstart_type, "
-                                       "dtstart_utime, "
-                                       "dtstart_datetime, "
-                                       "dtstart_tzid, "
-                                       "dtend_type, "
-                                       "dtend_utime, "
-                                       "dtend_datetime, "
-                                       "dtend_tzid"
-                                       ") SELECT %d,"
-                                       "created_ver, %d, "
-                                       "calendar_id, "
-                                       "%d, "
-                                       "'%s', "
-                                       "1, "
-                                       "dtstart_type, "
-                                       "%lld, "
-                                       "'%s', "
-                                       "dtstart_tzid, "
-                                       "dtend_type, "
-                                       "%lld+(dtend_utime-dtstart_utime), "
-                                       "'%s', "
-                                       "dtend_tzid "
-                                       "FROM %s "
-                                       "WHERE id = %d; ",
-                               CAL_TABLE_SCHEDULE,
-                               CAL_SCH_TYPE_EVENT,
-                               input_ver,
-                               event_id,
-                               patterns1[i],
-                               start_utime,
-                               datetime,
-                               start_utime,
-                               datetime,
-                               CAL_TABLE_SCHEDULE,
-                               event_id
-                                       );
+
+                       snprintf(query, sizeof(query), "INSERT INTO "CAL_TABLE_SCHEDULE" ("
+                                       "type, created_ver, changed_ver, calendar_id, original_event_id, "
+                                       "recurrence_id, is_deleted, dtstart_type, dtstart_utime, "
+                                       "dtstart_datetime, dtstart_tzid, dtend_type, dtend_utime, "
+                                       "dtend_datetime, dtend_tzid"
+                                       ") SELECT %d, created_ver, %d, calendar_id, %d, '%s', 1, dtstart_type, "
+                                       "%lld, '%s', dtstart_tzid, dtend_type, %lld+(dtend_utime-dtstart_utime), "
+                                       "'%s', dtend_tzid "
+                                       "FROM "CAL_TABLE_SCHEDULE" WHERE id = %d ",
+                                       CAL_SCH_TYPE_EVENT, input_ver, event_id, t_inserted[i], start_utime,
+                                       datetime, start_utime, datetime, event_id);
                        ret = cal_db_util_query_exec(query);
                        if (CALENDAR_ERROR_NONE != ret) {
                                /* LCOV_EXCL_START */
@@ -2725,7 +2703,7 @@ static int _cal_db_event_exdate_insert_utime(int event_id, const char* original_
                                /* LCOV_EXCL_STOP */
                        }
                        int event_id = cal_db_util_last_insert_id();
-                       DBG("last id(%d)", event_id);
+                       SECURE("event id(%d)", event_id);
                        ids[exception_count] = event_id;
                        exception_count++;
                }
@@ -2738,7 +2716,10 @@ static int _cal_db_event_exdate_insert_utime(int event_id, const char* original_
        if (exception_len)
                *exception_len = exception_count;
 
-       g_strfreev(patterns1);
-       g_strfreev(patterns2);
+       if (needs_update)
+               *needs_update = len_inserted > len_original ? true : false;
+
+       g_strfreev(t_inserted);
+       g_strfreev(t_original);
        return ret;
 }