Fix bug: reduce link count when record is linked another base record 01/156001/3
authorJeesun Kim <iamjs.kim@samsung.com>
Tue, 17 Oct 2017 02:42:34 +0000 (11:42 +0900)
committerJeesun Kim <iamjs.kim@samsung.com>
Tue, 17 Oct 2017 02:50:45 +0000 (11:50 +0900)
Change-Id: I21fffa71efdb8d7f545bd804f43512e49ff5fd4e

server/db/cal_db_link.c

index bbedd28..b1c33fd 100644 (file)
@@ -30,7 +30,7 @@
 #include "cal_db_plugin_alarm_helper.h"
 #include "cal_db_plugin_extended_helper.h"
 
-int cal_db_link_record(int base_id, int record_id)
+int cal_db_link_record(int base_id, int linked_id)
 {
        int ret = 0;
        char query[CAL_DB_SQL_MAX_LEN] = {0};
@@ -39,7 +39,7 @@ int cal_db_link_record(int base_id, int record_id)
        RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "cal_db_util_begin_trans() Fail");
 
        calendar_record_h record = NULL;
-       ret = cal_db_get_record(_calendar_event._uri, record_id, &record);
+       ret = cal_db_get_record(_calendar_event._uri, linked_id, &record);
        if (CALENDAR_ERROR_NONE != ret) {
                /* LCOV_EXCL_START */
                ERR("cal_db_get_record() Fail(%d)", ret);
@@ -47,12 +47,34 @@ int cal_db_link_record(int base_id, int record_id)
                return ret;
                /* LCOV_EXCL_STOP */
        }
+       cal_record_s *record_s = (cal_record_s *)record;
+       if (CAL_STRING_EQUAL != strcmp(record_s->view_uri, CALENDAR_VIEW_EVENT)) {
+               /* LCOV_EXCL_START */
+               DBG("This link is invalid in event");
+               calendar_record_destroy(record, true);
+               cal_db_util_end_trans(false);
+               return CALENDAR_ERROR_INVALID_PARAMETER;
+               /* LCOV_EXCL_STOP */
+       }
 
-       /* base record and linked record have even type */
+       /* if linked record already has base, reduce base linked count. */
+       snprintf(query, sizeof(query), "UPDATE "CAL_TABLE_SCHEDULE" SET link_count = link_count -1 "
+                       "WHERE id = (SELECT link_base_id FROM "CAL_TABLE_SCHEDULE" WHERE id = %d) AND type = %d "
+                       "AND (SELECT link_base_id FROM "CAL_TABLE_SCHEDULE" WHERE id = %d) > 0 ",
+                       linked_id, CALENDAR_BOOK_TYPE_EVENT, linked_id);
+
+       ret = cal_db_util_query_exec(query);
+       if (CALENDAR_ERROR_NONE != ret) {
+               /* LCOV_EXCL_START */
+               DBG("Base is not linked before(%d)", ret);
+               /* LCOV_EXCL_STOP */
+       }
+
+       /* base record and linked record have event type */
        snprintf(query, sizeof(query), "UPDATE "CAL_TABLE_SCHEDULE" SET link_base_id = %d "
                        "WHERE id = %d AND type = %d "
                        "AND (SELECT type FROM "CAL_TABLE_SCHEDULE" WHERE id = %d) = %d ",
-                       base_id, record_id, CALENDAR_BOOK_TYPE_EVENT, base_id, CALENDAR_BOOK_TYPE_EVENT);
+                       base_id, linked_id, CALENDAR_BOOK_TYPE_EVENT, base_id, CALENDAR_BOOK_TYPE_EVENT);
 
        ret = cal_db_util_query_exec(query);
        if (CALENDAR_ERROR_NONE != ret) {