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.
23 #include "cal_internal.h"
24 #include "cal_typedef.h"
26 #include "cal_record.h"
29 #include "cal_db_util.h"
31 #include "cal_db_query.h"
32 #include "cal_db_rrule.h"
33 #include "cal_db_plugin_alarm_helper.h"
34 #include "cal_db_plugin_attendee_helper.h"
35 #include "cal_db_plugin_extended_helper.h"
36 #include "cal_access_control.h"
37 #include "cal_utils.h"
39 static int _cal_db_todo_insert_record(calendar_record_h record, int* id);
40 static int _cal_db_todo_get_record(int id, calendar_record_h* out_record);
41 static int _cal_db_todo_update_record(calendar_record_h record);
42 static int _cal_db_todo_delete_record(int id);
43 static int _cal_db_todo_get_all_records(int offset, int limit, calendar_list_h* out_list);
44 static int _cal_db_todo_get_records_with_query(calendar_query_h query, int offset, int limit, calendar_list_h* out_list);
45 static int _cal_db_todo_delete_records(int ids[], int count);
46 static int _cal_db_todo_get_count(int *out_count);
47 static int _cal_db_todo_get_count_with_query(calendar_query_h query, int *out_count);
48 static int _cal_db_todo_replace_record(calendar_record_h record, int id);
49 static int _cal_db_todo_replace_records(const calendar_list_h list, int ids[], int count);
54 static void _cal_db_todo_get_stmt(sqlite3_stmt *stmt, bool is_view_table, calendar_record_h record, int *extended);
55 static void _cal_db_todo_get_property_stmt(sqlite3_stmt *stmt,
56 unsigned int property, int *stmt_count, calendar_record_h record);
57 static void _cal_db_todo_get_projection_stmt(sqlite3_stmt *stmt,
58 const unsigned int *projection, const int projection_count,
59 calendar_record_h record);
60 static int _cal_db_todo_update_dirty(calendar_record_h record);
61 static int _cal_db_todo_get_deleted_data(int id, int* calendar_book_id, int* created_ver);
62 static bool _cal_db_todo_check_calendar_book_type(calendar_record_h record);
64 cal_db_plugin_cb_s cal_db_todo_plugin_cb = {
65 .is_query_only = false,
66 .insert_record = _cal_db_todo_insert_record,
67 .get_record = _cal_db_todo_get_record,
68 .update_record = _cal_db_todo_update_record,
69 .delete_record = _cal_db_todo_delete_record,
70 .get_all_records = _cal_db_todo_get_all_records,
71 .get_records_with_query = _cal_db_todo_get_records_with_query,
72 .insert_records = NULL,
73 .update_records = NULL,
74 .delete_records = _cal_db_todo_delete_records,
75 .get_count = _cal_db_todo_get_count,
76 .get_count_with_query = _cal_db_todo_get_count_with_query,
77 .replace_record = _cal_db_todo_replace_record,
78 .replace_records = _cal_db_todo_replace_records
81 static int _cal_db_todo_insert_record(calendar_record_h record, int* id)
86 char query[CAL_DB_SQL_MAX_LEN] = {0};
87 char dtstart_datetime[CAL_STR_SHORT_LEN32] = {0};
88 char dtend_datetime[CAL_STR_SHORT_LEN32] = {0};
89 sqlite3_stmt *stmt = NULL;
90 cal_todo_s* todo = (cal_todo_s*)(record);
91 cal_rrule_s *rrule = NULL;
93 int calendar_book_id = 0;
94 calendar_record_h record_calendar = NULL;
97 RETV_IF(NULL == todo, CALENDAR_ERROR_INVALID_PARAMETER);
98 RETV_IF(false == _cal_db_todo_check_calendar_book_type(record), CALENDAR_ERROR_INVALID_PARAMETER);
100 if (cal_access_control_have_write_permission(todo->calendar_id) == false) {
101 /* LCOV_EXCL_START */
102 ERR("cal_access_control_have_write_permission() Fail");
103 return CALENDAR_ERROR_PERMISSION_DENIED;
107 ret = calendar_record_get_int(record,
108 _calendar_todo.calendar_book_id, &calendar_book_id);
109 DBG("calendar_book_id(%d)", calendar_book_id);
111 ret = cal_db_get_record(_calendar_book._uri,
112 calendar_book_id, &record_calendar);
113 RETVM_IF(CALENDAR_ERROR_NONE != ret, CALENDAR_ERROR_INVALID_PARAMETER, "calendar_book_id is invalid");
115 calendar_record_destroy(record_calendar, true);
117 has_alarm = cal_db_alarm_has_alarm(todo->alarm_list);
118 input_ver = cal_db_util_get_next_ver();
120 if (CALENDAR_TIME_LOCALTIME == todo->start.type
121 && (0 == todo->start.time.date.hour)
122 && (0 == todo->start.time.date.minute)
123 && (0 == todo->start.time.date.second)
124 && (0 == todo->due.time.date.hour)
125 && (0 == todo->due.time.date.minute)
126 && (0 == todo->due.time.date.second)) {
130 ret = snprintf(query, sizeof(query),
133 "created_ver, changed_ver, "
134 "summary, description, location, categories, "
135 "task_status, priority, "
138 "latitude, longitude, "
139 "created_time, completed_time, progress, "
140 "dtstart_type, dtstart_utime, dtstart_datetime, dtstart_tzid, "
141 "dtend_type, dtend_utime, dtend_datetime, dtend_tzid, "
142 "last_mod, rrule_id, "
143 "has_alarm, system_type, updated, "
144 "sync_data1, sync_data2, sync_data3, sync_data4, "
145 "organizer_name, organizer_email, "
146 "has_attendee, has_extended, "
156 "strftime('%%s', 'now'), %lld, %d, "
159 "strftime('%%s', 'now'), %d, "
166 CAL_SCH_TYPE_TODO, /*event->cal_type,*/
167 input_ver, input_ver,
168 todo->todo_status, todo->priority,
171 todo->latitude, todo->longitude,
172 todo->completed_time, todo->progress,
173 todo->start.type, todo->start.type == CALENDAR_TIME_UTIME ? todo->start.time.utime : 0,
174 todo->due.type, todo->due.type == CALENDAR_TIME_UTIME ? todo->due.time.utime : 0,
175 0 < todo->freq ? 1 : 0,
179 (todo->attendee_list && 0 < todo->attendee_list->count) ? 1 : 0,
180 (todo->extended_list && 0 < todo->extended_list->count) ? 1 : 0,
181 todo->freq, is_allday);
183 ret = cal_db_util_query_prepare(query, &stmt);
184 if (CALENDAR_ERROR_NONE != ret) {
185 /* LCOV_EXCL_START */
186 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
187 SECURE("query[%s]", query);
195 cal_db_util_stmt_bind_text(stmt, count, todo->summary);
198 if (todo->description)
199 cal_db_util_stmt_bind_text(stmt, count, todo->description);
203 cal_db_util_stmt_bind_text(stmt, count, todo->location);
206 if (todo->categories)
207 cal_db_util_stmt_bind_text(stmt, count, todo->categories);
211 cal_db_util_stmt_bind_text(stmt, count, todo->uid);
214 if (CALENDAR_TIME_LOCALTIME == todo->start.type) {
215 snprintf(dtstart_datetime, sizeof(dtstart_datetime), CAL_FORMAT_LOCAL_DATETIME,
216 todo->start.time.date.year,
217 todo->start.time.date.month,
218 todo->start.time.date.mday,
219 todo->start.time.date.hour,
220 todo->start.time.date.minute,
221 todo->start.time.date.second);
222 cal_db_util_stmt_bind_text(stmt, count, dtstart_datetime);
226 if (todo->start_tzid)
227 cal_db_util_stmt_bind_text(stmt, count, todo->start_tzid);
230 if (CALENDAR_TIME_LOCALTIME == todo->due.type) {
231 snprintf(dtend_datetime, sizeof(dtend_datetime), CAL_FORMAT_LOCAL_DATETIME,
232 todo->due.time.date.year,
233 todo->due.time.date.month,
234 todo->due.time.date.mday,
235 todo->due.time.date.hour,
236 todo->due.time.date.minute,
237 todo->due.time.date.second);
238 cal_db_util_stmt_bind_text(stmt, count, dtend_datetime);
243 cal_db_util_stmt_bind_text(stmt, count, todo->due_tzid);
246 if (todo->sync_data1)
247 cal_db_util_stmt_bind_text(stmt, count, todo->sync_data1);
249 if (todo->sync_data2)
250 cal_db_util_stmt_bind_text(stmt, count, todo->sync_data2);
252 if (todo->sync_data3)
253 cal_db_util_stmt_bind_text(stmt, count, todo->sync_data3);
255 if (todo->sync_data4)
256 cal_db_util_stmt_bind_text(stmt, count, todo->sync_data4);
258 if (todo->organizer_name)
259 cal_db_util_stmt_bind_text(stmt, count, todo->organizer_name);
261 if (todo->organizer_email)
262 cal_db_util_stmt_bind_text(stmt, count, todo->organizer_email);
265 ret = cal_db_util_stmt_step(stmt);
266 if (CALENDAR_ERROR_NONE != ret) {
267 /* LCOV_EXCL_START */
268 ERR("cal_db_util_stmt_step() Fail(%d)", ret);
269 SECURE("query[%s]", query);
270 sqlite3_finalize(stmt);
275 index = cal_db_util_last_insert_id();
276 sqlite3_finalize(stmt);
278 calendar_record_get_int(record, _calendar_todo.id, &tmp);
279 cal_record_set_int(record, _calendar_todo.id, index);
283 cal_db_rrule_get_rrule_from_record(record, &rrule);
284 cal_db_rrule_insert_record(index, rrule);
286 if (todo->alarm_list && 0 < todo->alarm_list->count) {
287 ret = cal_db_alarm_insert_records(todo->alarm_list, index);
288 WARN_IF(CALENDAR_ERROR_NONE != ret, "cal_db_alarm_insert_records() Fail(%x)", ret);
291 if (todo->attendee_list && 0 < todo->attendee_list->count) {
292 ret = cal_db_attendee_insert_records(todo->attendee_list, index);
293 WARN_IF(CALENDAR_ERROR_NONE != ret, "cal_db_attendee_insert_records() Fail(%x)", ret);
296 if (todo->extended_list && 0 < todo->extended_list->count) {
297 DBG("insert extended");
298 ret = cal_db_extended_insert_records(todo->extended_list, index, CALENDAR_RECORD_TYPE_TODO);
299 WARN_IF(CALENDAR_ERROR_NONE != ret, "cal_db_extended_insert_records() Fail(%x)", ret);
305 cal_db_util_notify(CAL_NOTI_TYPE_TODO);
307 cal_record_set_int(record, _calendar_todo.id, tmp);
309 return CALENDAR_ERROR_NONE;
312 static int _cal_db_todo_get_record(int id, calendar_record_h* out_record)
314 char query[CAL_DB_SQL_MAX_LEN];
315 cal_todo_s *todo = NULL;
316 cal_rrule_s *rrule = NULL;
317 sqlite3_stmt *stmt = NULL;
319 calendar_record_h record_calendar;
320 calendar_book_sync_event_type_e sync_event_type = CALENDAR_BOOK_SYNC_EVENT_FOR_ME;
323 ret = calendar_record_create(_calendar_todo._uri, out_record);
324 if (CALENDAR_ERROR_NONE != ret) {
325 /* LCOV_EXCL_START */
326 ERR("calendar_record_create() Fail(%d)", ret);
327 return CALENDAR_ERROR_OUT_OF_MEMORY;
331 todo = (cal_todo_s*)(*out_record);
333 snprintf(query, sizeof(query), "SELECT "CAL_QUERY_SCHEDULE_A_ALL" FROM %s AS A "
334 "WHERE id=%d AND (type = %d OR type = %d) AND calendar_id IN "
335 "(select id from %s where deleted = 0)",
337 id, CALENDAR_BOOK_TYPE_TODO, CALENDAR_BOOK_TYPE_NONE,
339 ret = cal_db_util_query_prepare(query, &stmt);
340 if (CALENDAR_ERROR_NONE != ret) {
341 /* LCOV_EXCL_START */
342 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
343 SECURE("query[%s]", query);
344 calendar_record_destroy(*out_record, true);
350 ret = cal_db_util_stmt_step(stmt);
351 if (CAL_SQLITE_ROW != ret) {
352 /* LCOV_EXCL_START */
353 ERR("cal_db_util_stmt_step() Fail(%d)", ret);
354 sqlite3_finalize(stmt);
355 calendar_record_destroy(*out_record, true);
357 if (CALENDAR_ERROR_NONE == ret)
358 return CALENDAR_ERROR_DB_RECORD_NOT_FOUND;
363 _cal_db_todo_get_stmt(stmt, false, *out_record, &extended);
364 sqlite3_finalize(stmt);
367 ret = cal_db_get_record(_calendar_book._uri, todo->calendar_id, &record_calendar);
368 if (CALENDAR_ERROR_NONE == ret) {
369 ret = calendar_record_get_int(record_calendar,
370 _calendar_book.sync_event, (int *)&sync_event_type);
371 calendar_record_destroy(record_calendar, true);
373 if (todo->is_deleted == 1 && sync_event_type != CALENDAR_BOOK_SYNC_EVENT_FOR_EVERY_AND_REMAIN
375 calendar_record_destroy(*out_record, true);
377 return CALENDAR_ERROR_DB_RECORD_NOT_FOUND;
380 if (CALENDAR_RECURRENCE_NONE != todo->freq) {
381 ret = cal_db_rrule_get_rrule(todo->index, &rrule);
382 if (CALENDAR_ERROR_NONE == ret) {
383 cal_db_rrule_set_rrule_to_record(rrule, *out_record);
388 if (todo->has_alarm == 1)
389 cal_db_alarm_get_records(todo->index, todo->alarm_list);
391 if (todo->has_attendee == 1)
392 cal_db_attendee_get_records(todo->index, todo->attendee_list);
395 cal_db_extended_get_records(todo->index, CALENDAR_RECORD_TYPE_TODO, todo->extended_list);
397 return CALENDAR_ERROR_NONE;
400 static int _cal_db_todo_update_record(calendar_record_h record)
402 char query[CAL_DB_SQL_MAX_LEN] = {0};
403 char dtstart_datetime[CAL_STR_SHORT_LEN32] = {0};
404 char dtend_datetime[CAL_STR_SHORT_LEN32] = {0};
405 sqlite3_stmt *stmt = NULL;
406 cal_todo_s* todo = (cal_todo_s*)(record);
407 cal_rrule_s *rrule = NULL;
411 RETV_IF(NULL == todo, CALENDAR_ERROR_INVALID_PARAMETER);
413 if (cal_access_control_have_write_permission(todo->calendar_id) == false) {
414 /* LCOV_EXCL_START */
415 ERR("cal_access_control_have_write_permission() Fail");
416 return CALENDAR_ERROR_PERMISSION_DENIED;
420 if (todo->common.properties_flags != NULL)
421 return _cal_db_todo_update_dirty(record);
423 has_alarm = cal_db_alarm_has_alarm(todo->alarm_list);
425 if (CALENDAR_TIME_LOCALTIME == todo->start.type
426 && (0 == todo->start.time.date.hour)
427 && (0 == todo->start.time.date.minute)
428 && (0 == todo->start.time.date.second)
429 && (0 == todo->due.time.date.hour)
430 && (0 == todo->due.time.date.minute)
431 && (0 == todo->due.time.date.second)) {
435 snprintf(query, sizeof(query), "UPDATE %s SET "
449 "completed_time = %lld,"
451 "dtstart_type = %d, "
452 "dtstart_utime = %lld, "
453 "dtstart_datetime = ?, "
456 "dtend_utime = %lld, "
457 "dtend_datetime = ?, "
459 "last_mod = strftime('%%s', 'now'), "
467 "organizer_name = ?, "
468 "organizer_email = ?, "
470 "has_extended = %d, "
474 cal_db_util_get_next_ver(),
475 CAL_SCH_TYPE_TODO,/*todo->cal_type,*/
482 todo->completed_time,
485 todo->start.type == CALENDAR_TIME_UTIME ? todo->start.time.utime : 0,
487 todo->due.type == CALENDAR_TIME_UTIME ? todo->due.time.utime : 0,
491 (todo->attendee_list && 0 < todo->attendee_list->count) ? 1 : 0,
492 (todo->extended_list && 0 < todo->extended_list->count) ? 1 : 0,
496 ret = cal_db_util_query_prepare(query, &stmt);
497 if (CALENDAR_ERROR_NONE != ret) {
498 /* LCOV_EXCL_START */
499 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
500 SECURE("query[%s]", query);
508 cal_db_util_stmt_bind_text(stmt, count, todo->summary);
511 if (todo->description)
512 cal_db_util_stmt_bind_text(stmt, count, todo->description);
516 cal_db_util_stmt_bind_text(stmt, count, todo->location);
519 if (todo->categories)
520 cal_db_util_stmt_bind_text(stmt, count, todo->categories);
524 cal_db_util_stmt_bind_text(stmt, count, todo->uid);
527 if (CALENDAR_TIME_LOCALTIME == todo->start.type) {
528 snprintf(dtstart_datetime, sizeof(dtstart_datetime), CAL_FORMAT_LOCAL_DATETIME,
529 todo->start.time.date.year,
530 todo->start.time.date.month,
531 todo->start.time.date.mday,
532 todo->start.time.date.hour,
533 todo->start.time.date.minute,
534 todo->start.time.date.second);
535 cal_db_util_stmt_bind_text(stmt, count, dtstart_datetime);
539 if (todo->start_tzid)
540 cal_db_util_stmt_bind_text(stmt, count, todo->start_tzid);
543 if (CALENDAR_TIME_LOCALTIME == todo->due.type) {
544 snprintf(dtend_datetime, sizeof(dtend_datetime), CAL_FORMAT_LOCAL_DATETIME,
545 todo->due.time.date.year,
546 todo->due.time.date.month,
547 todo->due.time.date.mday,
548 todo->due.time.date.hour,
549 todo->due.time.date.minute,
550 todo->due.time.date.second);
551 cal_db_util_stmt_bind_text(stmt, count, dtend_datetime);
556 cal_db_util_stmt_bind_text(stmt, count, todo->due_tzid);
559 if (todo->sync_data1)
560 cal_db_util_stmt_bind_text(stmt, count, todo->sync_data1);
562 if (todo->sync_data2)
563 cal_db_util_stmt_bind_text(stmt, count, todo->sync_data2);
565 if (todo->sync_data3)
566 cal_db_util_stmt_bind_text(stmt, count, todo->sync_data3);
568 if (todo->sync_data4)
569 cal_db_util_stmt_bind_text(stmt, count, todo->sync_data4);
571 if (todo->organizer_name)
572 cal_db_util_stmt_bind_text(stmt, count, todo->organizer_name);
574 if (todo->organizer_email)
575 cal_db_util_stmt_bind_text(stmt, count, todo->organizer_email);
578 ret = cal_db_util_stmt_step(stmt);
579 sqlite3_finalize(stmt);
580 if (CALENDAR_ERROR_NONE != ret) {
581 /* LCOV_EXCL_START */
582 ERR("cal_db_util_stmt_step() Fail(%d)", ret);
587 cal_db_rrule_get_rrule_from_record(record, &rrule);
588 cal_db_rrule_update_record(todo->index, rrule);
591 cal_db_alarm_delete_with_id(todo->index);
592 cal_db_attendee_delete_with_id(todo->index);
593 cal_db_extended_delete_with_id(todo->index, CALENDAR_RECORD_TYPE_TODO);
595 if (todo->alarm_list && 0 < todo->alarm_list->count) {
596 ret = cal_db_alarm_insert_records(todo->alarm_list, todo->index);
597 WARN_IF(CALENDAR_ERROR_NONE != ret, "cal_db_alarm_insert_records() Fail(%d)", ret);
600 if (todo->attendee_list && 0 < todo->attendee_list->count) {
601 ret = cal_db_attendee_insert_records(todo->attendee_list, todo->index);
602 WARN_IF(CALENDAR_ERROR_NONE != ret, "cal_db_attendee_insert_records() Fail(%d)", ret);
606 if (todo->extended_list && 0 < todo->extended_list->count) {
607 DBG("insert extended");
608 ret = cal_db_extended_insert_records(todo->extended_list, todo->index, CALENDAR_RECORD_TYPE_TODO);
609 WARN_IF(CALENDAR_ERROR_NONE != ret, "cal_db_extended_insert_records() Fail(%d)", ret);
612 cal_db_util_notify(CAL_NOTI_TYPE_TODO);
614 return CALENDAR_ERROR_NONE;
617 static int _cal_db_todo_delete_record(int id)
620 int calendar_book_id = 0;
621 char query[CAL_DB_SQL_MAX_LEN] = {0};
623 calendar_book_sync_event_type_e sync_event_type = CALENDAR_BOOK_SYNC_EVENT_FOR_ME;
625 RETVM_IF(id < 0, CALENDAR_ERROR_INVALID_PARAMETER, "id(%d) < 0", id);
627 ret = _cal_db_todo_get_deleted_data(id, &calendar_book_id, &created_ver);
628 if (CALENDAR_ERROR_NONE != ret) {
629 DBG("_cal_db_event_get_deleted_data() Fail");
633 if (cal_access_control_have_write_permission(calendar_book_id) == false) {
634 /* LCOV_EXCL_START */
635 ERR("cal_access_control_have_write_permission() Fail");
636 return CALENDAR_ERROR_PERMISSION_DENIED;
640 snprintf(query, sizeof(query), "SELECT sync_event FROM %s WHERE id = %d ",
641 CAL_TABLE_CALENDAR, calendar_book_id);
642 ret = cal_db_util_query_get_first_int_result(query, NULL, (int *)&sync_event_type);
643 if (CALENDAR_ERROR_NONE != ret) {
644 /* LCOV_EXCL_START */
645 ERR("cal_db_util_query_get_first_int_result() Fail");
649 DBG("sync_event_type(%d)", sync_event_type);
651 if (sync_event_type == CALENDAR_BOOK_SYNC_EVENT_FOR_EVERY_AND_REMAIN) {
652 DBG("set is_delete");
653 snprintf(query, sizeof(query), "UPDATE %s SET is_deleted = 1, changed_ver = %d, "
654 "last_mod = strftime('%%s','now') WHERE id = %d ",
655 CAL_TABLE_SCHEDULE, cal_db_util_get_next_ver(), id);
657 ret = cal_db_util_query_exec(query);
658 if (CALENDAR_ERROR_NONE != ret) {
659 /* LCOV_EXCL_START */
660 ERR("cal_db_util_query_exec() Fail(%d)", ret);
661 SECURE("[%s]", query);
665 DBG("attendee, alarm and rrule will be deleted by trigger after sync clean");
667 cal_db_util_get_next_ver();
670 snprintf(query, sizeof(query), "DELETE FROM %s WHERE id = %d ", CAL_TABLE_SCHEDULE, id);
671 ret = cal_db_util_query_exec(query);
672 if (CALENDAR_ERROR_NONE != ret) {
673 /* LCOV_EXCL_START */
674 ERR("cal_db_util_query_exec() Fail(%d)", ret);
675 SECURE("[%s]", query);
679 DBG("attendee, alarm and rrule is deleted by trigger");
681 cal_db_util_notify(CAL_NOTI_TYPE_TODO);
683 return CALENDAR_ERROR_NONE;
686 static int _cal_db_todo_replace_record(calendar_record_h record, int id)
688 int ret = CALENDAR_ERROR_NONE;
689 char query[CAL_DB_SQL_MAX_LEN] = {0};
690 char dtstart_datetime[CAL_STR_SHORT_LEN32] = {0};
691 char dtend_datetime[CAL_STR_SHORT_LEN32] = {0};
692 sqlite3_stmt *stmt = NULL;
693 cal_todo_s* todo = (cal_todo_s*)(record);
694 cal_rrule_s *rrule = NULL;
697 RETV_IF(NULL == todo, CALENDAR_ERROR_INVALID_PARAMETER);
700 if (cal_access_control_have_write_permission(todo->calendar_id) == false) {
701 /* LCOV_EXCL_START */
702 ERR("cal_access_control_have_write_permission() Fail");
703 return CALENDAR_ERROR_PERMISSION_DENIED;
707 if (todo->common.properties_flags)
708 return _cal_db_todo_update_dirty(record);
711 if (CALENDAR_TIME_LOCALTIME == todo->start.type
712 && (0 == todo->start.time.date.hour)
713 && (0 == todo->start.time.date.minute)
714 && (0 == todo->start.time.date.second)
715 && (0 == todo->due.time.date.hour)
716 && (0 == todo->due.time.date.minute)
717 && (0 == todo->due.time.date.second)) {
721 has_alarm = cal_db_alarm_has_alarm(todo->alarm_list);
722 snprintf(query, sizeof(query), "UPDATE %s SET "
736 "completed_time = %lld,"
738 "dtstart_type = %d, "
739 "dtstart_utime = %lld, "
740 "dtstart_datetime = ?, "
743 "dtend_utime = %lld, "
744 "dtend_datetime = ?, "
746 "last_mod = strftime('%%s', 'now'), "
754 "organizer_name = ?, "
755 "organizer_email = ?, "
757 "has_extended = %d, "
761 cal_db_util_get_next_ver(),
762 CAL_SCH_TYPE_TODO,/*todo->cal_type,*/
769 todo->completed_time,
772 todo->start.type == CALENDAR_TIME_UTIME ? todo->start.time.utime : 0,
774 todo->due.type == CALENDAR_TIME_UTIME ? todo->due.time.utime : 0,
778 (todo->attendee_list && 0 < todo->attendee_list->count) ? 1 : 0,
779 (todo->extended_list && 0 < todo->extended_list->count) ? 1 : 0,
783 ret = cal_db_util_query_prepare(query, &stmt);
784 if (CALENDAR_ERROR_NONE != ret) {
785 /* LCOV_EXCL_START */
786 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
787 SECURE("query[%s]", query);
795 cal_db_util_stmt_bind_text(stmt, count, todo->summary);
798 if (todo->description)
799 cal_db_util_stmt_bind_text(stmt, count, todo->description);
803 cal_db_util_stmt_bind_text(stmt, count, todo->location);
806 if (todo->categories)
807 cal_db_util_stmt_bind_text(stmt, count, todo->categories);
811 cal_db_util_stmt_bind_text(stmt, count, todo->uid);
814 if (CALENDAR_TIME_LOCALTIME == todo->start.type) {
815 snprintf(dtstart_datetime, sizeof(dtstart_datetime), CAL_FORMAT_LOCAL_DATETIME,
816 todo->start.time.date.year,
817 todo->start.time.date.month,
818 todo->start.time.date.mday,
819 todo->start.time.date.hour,
820 todo->start.time.date.minute,
821 todo->start.time.date.second);
822 cal_db_util_stmt_bind_text(stmt, count, dtstart_datetime);
826 if (todo->start_tzid)
827 cal_db_util_stmt_bind_text(stmt, count, todo->start_tzid);
830 if (CALENDAR_TIME_LOCALTIME == todo->due.type) {
831 snprintf(dtend_datetime, sizeof(dtend_datetime), CAL_FORMAT_LOCAL_DATETIME,
832 todo->due.time.date.year,
833 todo->due.time.date.month,
834 todo->due.time.date.mday,
835 todo->due.time.date.hour,
836 todo->due.time.date.minute,
837 todo->due.time.date.second);
838 cal_db_util_stmt_bind_text(stmt, count, dtend_datetime);
843 cal_db_util_stmt_bind_text(stmt, count, todo->due_tzid);
846 if (todo->sync_data1)
847 cal_db_util_stmt_bind_text(stmt, count, todo->sync_data1);
849 if (todo->sync_data2)
850 cal_db_util_stmt_bind_text(stmt, count, todo->sync_data2);
852 if (todo->sync_data3)
853 cal_db_util_stmt_bind_text(stmt, count, todo->sync_data3);
855 if (todo->sync_data4)
856 cal_db_util_stmt_bind_text(stmt, count, todo->sync_data4);
858 if (todo->organizer_name)
859 cal_db_util_stmt_bind_text(stmt, count, todo->organizer_name);
861 if (todo->organizer_email)
862 cal_db_util_stmt_bind_text(stmt, count, todo->organizer_email);
865 ret = cal_db_util_stmt_step(stmt);
866 sqlite3_finalize(stmt);
867 if (CALENDAR_ERROR_NONE != ret) {
868 /* LCOV_EXCL_START */
869 ERR("cal_db_util_stmt_step() Fail(%d)", ret);
874 cal_db_rrule_get_rrule_from_record(record, &rrule);
875 cal_db_rrule_update_record(id, rrule);
878 cal_db_alarm_delete_with_id(id);
879 cal_db_attendee_delete_with_id(id);
880 cal_db_extended_delete_with_id(id, CALENDAR_RECORD_TYPE_TODO);
882 if (todo->alarm_list && 0 < todo->alarm_list->count) {
883 ret = cal_db_alarm_insert_records(todo->alarm_list, id);
884 WARN_IF(CALENDAR_ERROR_NONE != ret, "cal_db_alarm_insert_records() Fail(%x)", ret);
887 if (todo->attendee_list && 0 < todo->attendee_list->count) {
888 ret = cal_db_attendee_insert_records(todo->attendee_list, id);
889 WARN_IF(CALENDAR_ERROR_NONE != ret, "cal_db_attendee_insert_records() Fail(%x)", ret);
892 if (todo->extended_list && 0 < todo->extended_list->count) {
893 DBG("insert extended");
894 ret = cal_db_extended_insert_records(todo->extended_list, id, CALENDAR_RECORD_TYPE_TODO);
895 WARN_IF(CALENDAR_ERROR_NONE != ret, "cal_db_extended_insert_records() Fail(%d)", ret);
898 cal_db_util_notify(CAL_NOTI_TYPE_TODO);
900 return CALENDAR_ERROR_NONE;
903 static int _cal_db_todo_get_all_records(int offset, int limit, calendar_list_h* out_list)
905 int ret = CALENDAR_ERROR_NONE;
906 char offsetquery[CAL_DB_SQL_MAX_LEN] = {0};
907 char limitquery[CAL_DB_SQL_MAX_LEN] = {0};
908 sqlite3_stmt *stmt = NULL;
910 RETV_IF(NULL == out_list, CALENDAR_ERROR_INVALID_PARAMETER);
912 ret = calendar_list_create(out_list);
913 RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "calendar_list_create() Fail(%d)", ret);
916 snprintf(offsetquery, sizeof(offsetquery), "OFFSET %d", offset);
919 snprintf(limitquery, sizeof(limitquery), "LIMIT %d", limit);
921 char *query_str = NULL;
922 cal_db_append_string(&query_str, "SELECT * FROM");
923 cal_db_append_string(&query_str, CAL_VIEW_TABLE_TODO);
924 cal_db_append_string(&query_str, limitquery);
925 cal_db_append_string(&query_str, offsetquery);
927 ret = cal_db_util_query_prepare(query_str, &stmt);
928 if (CALENDAR_ERROR_NONE != ret) {
929 /* LCOV_EXCL_START */
930 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
931 SECURE("query[%s]", query_str);
932 calendar_list_destroy(*out_list, true);
939 while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
940 calendar_record_h record;
942 ret = calendar_record_create(_calendar_todo._uri, &record);
943 if (CALENDAR_ERROR_NONE != ret) {
944 /* LCOV_EXCL_START */
945 calendar_list_destroy(*out_list, true);
947 sqlite3_finalize(stmt);
952 _cal_db_todo_get_stmt(stmt, true, record, &extended);
955 int has_attendee = 0, has_alarm = 0;
957 cal_todo_s* ptodo = (cal_todo_s*) record;
958 calendar_record_get_int(record, _calendar_todo.id, &record_id);
959 if (CALENDAR_ERROR_NONE == calendar_record_get_int(record, _calendar_todo.has_attendee, &has_attendee)) {
960 if (has_attendee == 1)
961 cal_db_attendee_get_records(record_id, ptodo->attendee_list);
963 if (CALENDAR_ERROR_NONE == calendar_record_get_int(record, _calendar_todo.has_alarm, &has_alarm)) {
965 cal_db_alarm_get_records(record_id, ptodo->alarm_list);
969 cal_db_extended_get_records(record_id, CALENDAR_RECORD_TYPE_TODO, ptodo->extended_list);
971 ret = calendar_list_add(*out_list, record);
972 if (CALENDAR_ERROR_NONE != ret) {
973 /* LCOV_EXCL_START */
974 calendar_list_destroy(*out_list, true);
976 calendar_record_destroy(record, true);
977 sqlite3_finalize(stmt);
983 sqlite3_finalize(stmt);
986 return CALENDAR_ERROR_NONE;
989 static int _cal_db_todo_get_records_with_query(calendar_query_h query, int offset, int limit, calendar_list_h* out_list)
991 cal_query_s *que = NULL;
992 int ret = CALENDAR_ERROR_NONE;
993 char *condition = NULL;
994 char *projection = NULL;
995 GSList *bind_text = NULL, *cursor = NULL;
996 sqlite3_stmt *stmt = NULL;
1000 que = (cal_query_s *)query;
1002 if (CAL_STRING_EQUAL == strcmp(que->view_uri, CALENDAR_VIEW_TODO)) {
1003 table_name = cal_strdup(CAL_VIEW_TABLE_TODO);
1004 } else if (CAL_STRING_EQUAL == strcmp(que->view_uri, CALENDAR_VIEW_TODO_CALENDAR)) {
1005 table_name = cal_strdup(CAL_VIEW_TABLE_TODO_CALENDAR);
1007 /* LCOV_EXCL_START */
1008 ERR("uri(%s) not support get records with query", que->view_uri);
1009 return CALENDAR_ERROR_INVALID_PARAMETER;
1010 /* LCOV_EXCL_STOP */
1015 ret = cal_db_query_create_condition(query, &condition, &bind_text);
1016 if (CALENDAR_ERROR_NONE != ret) {
1017 /* LCOV_EXCL_START */
1018 ERR("cal_db_query_create_condition() Fail(%d), ret");
1019 CAL_FREE(table_name);
1021 /* LCOV_EXCL_STOP */
1025 /* make: projection */
1026 ret = cal_db_query_create_projection(query, &projection);
1028 char *query_str = NULL;
1030 /* query: projection */
1032 cal_db_append_string(&query_str, "SELECT");
1033 cal_db_append_string(&query_str, projection);
1034 cal_db_append_string(&query_str, "FROM");
1035 cal_db_append_string(&query_str, table_name);
1036 CAL_FREE(projection);
1038 cal_db_append_string(&query_str, "SELECT * FROM");
1039 cal_db_append_string(&query_str, table_name);
1041 CAL_FREE(table_name);
1043 /* query: condition */
1045 cal_db_append_string(&query_str, "WHERE (");
1046 cal_db_append_string(&query_str, condition);
1047 cal_db_append_string(&query_str, ")");
1052 ret = cal_db_query_create_order(query, condition, &order);
1054 cal_db_append_string(&query_str, order);
1057 CAL_FREE(condition);
1060 char buf[CAL_STR_SHORT_LEN32] = {0};
1062 snprintf(buf, sizeof(buf), "LIMIT %d", limit);
1063 cal_db_append_string(&query_str, buf);
1066 snprintf(buf, sizeof(buf), "OFFSET %d", offset);
1067 cal_db_append_string(&query_str, buf);
1072 ret = cal_db_util_query_prepare(query_str, &stmt);
1073 if (CALENDAR_ERROR_NONE != ret) {
1074 /* LCOV_EXCL_START */
1075 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
1076 SECURE("query[%s]", query_str);
1078 g_slist_free_full(bind_text, free);
1083 /* LCOV_EXCL_STOP */
1088 g_slist_length(bind_text);
1089 for (cursor = bind_text, i = 1; cursor; cursor = cursor->next, i++)
1090 cal_db_util_stmt_bind_text(stmt, i, cursor->data);
1093 ret = calendar_list_create(out_list);
1094 if (CALENDAR_ERROR_NONE != ret) {
1095 /* LCOV_EXCL_START */
1096 ERR("calendar_list_create() Fail");
1098 g_slist_free_full(bind_text, free);
1101 sqlite3_finalize(stmt);
1102 CAL_FREE(query_str);
1104 /* LCOV_EXCL_STOP */
1107 while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
1108 calendar_record_h record;
1110 int attendee = 1, alarm = 1;
1111 ret = calendar_record_create(_calendar_todo._uri, &record);
1112 if (CALENDAR_ERROR_NONE != ret) {
1113 /* LCOV_EXCL_START */
1114 calendar_list_destroy(*out_list, true);
1118 g_slist_free_full(bind_text, free);
1121 sqlite3_finalize(stmt);
1122 CAL_FREE(query_str);
1124 /* LCOV_EXCL_STOP */
1126 if (0 < que->projection_count) {
1127 cal_record_set_projection(record,
1128 que->projection, que->projection_count, que->property_count);
1130 _cal_db_todo_get_projection_stmt(stmt,
1131 que->projection, que->projection_count,
1134 cal_todo_s *todo = NULL;
1135 _cal_db_todo_get_stmt(stmt, true, record, &extended);
1136 todo = (cal_todo_s*)(record);
1138 attendee = todo->has_attendee;
1139 alarm = todo->has_alarm;
1144 if (cal_db_query_find_projection_property(query, CAL_PROPERTY_TODO_CALENDAR_ALARM) == true && alarm == 1) {
1145 cal_todo_s* todo = (cal_todo_s*) record;
1146 cal_db_alarm_get_records(todo->index, todo->alarm_list);
1148 if (cal_db_query_find_projection_property(query, CAL_PROPERTY_TODO_CALENDAR_ATTENDEE) == true && attendee == 1) {
1149 cal_todo_s* todo = (cal_todo_s*) record;
1150 cal_db_attendee_get_records(todo->index, todo->attendee_list);
1152 if (cal_db_query_find_projection_property(query, CAL_PROPERTY_TODO_EXTENDED) == true && extended == 1) {
1153 cal_todo_s* todo = (cal_todo_s*) record;
1154 cal_db_extended_get_records(todo->index, CALENDAR_RECORD_TYPE_TODO, todo->extended_list);
1157 ret = calendar_list_add(*out_list, record);
1158 if (CALENDAR_ERROR_NONE != ret) {
1159 /* LCOV_EXCL_START */
1160 calendar_list_destroy(*out_list, true);
1162 calendar_record_destroy(record, true);
1165 g_slist_free_full(bind_text, free);
1168 sqlite3_finalize(stmt);
1169 CAL_FREE(query_str);
1171 /* LCOV_EXCL_STOP */
1176 g_slist_free_full(bind_text, free);
1180 sqlite3_finalize(stmt);
1181 CAL_FREE(query_str);
1183 return CALENDAR_ERROR_NONE;
1186 static int _cal_db_todo_delete_records(int ids[], int count)
1190 for (i = 0; i < count; i++) {
1191 ret = _cal_db_todo_delete_record(ids[i]);
1192 if (CALENDAR_ERROR_NONE != ret) {
1193 /* LCOV_EXCL_START */
1194 ERR("_cal_db_todo_delete_record() Fail(%d)", ret);
1195 return CALENDAR_ERROR_DB_FAILED;
1196 /* LCOV_EXCL_STOP */
1199 return CALENDAR_ERROR_NONE;
1202 static int _cal_db_todo_get_count(int *out_count)
1204 RETV_IF(NULL == out_count, CALENDAR_ERROR_INVALID_PARAMETER);
1206 char *query_str = NULL;
1207 cal_db_append_string(&query_str, "SELECT count(*) FROM");
1208 cal_db_append_string(&query_str, CAL_VIEW_TABLE_TODO);
1212 ret = cal_db_util_query_get_first_int_result(query_str, NULL, &count);
1213 if (CALENDAR_ERROR_NONE != ret) {
1214 /* LCOV_EXCL_START */
1215 ERR("cal_db_util_query_get_first_int_result() Fail");
1216 CAL_FREE(query_str);
1218 /* LCOV_EXCL_STOP */
1220 DBG("count(%d) str[%s]", count, query_str);
1221 CAL_FREE(query_str);
1224 return CALENDAR_ERROR_NONE;
1227 static int _cal_db_todo_replace_records(const calendar_list_h list, int ids[], int count)
1229 calendar_record_h record;
1234 /* LCOV_EXCL_START */
1235 ERR("Invalid argument: list is NULL");
1236 return CALENDAR_ERROR_INVALID_PARAMETER;
1237 /* LCOV_EXCL_STOP */
1240 ret = calendar_list_first(list);
1241 if (CALENDAR_ERROR_NONE != ret) {
1242 /* LCOV_EXCL_START */
1243 ERR("list first error");
1245 /* LCOV_EXCL_STOP */
1248 for (i = 0; i < count; i++) {
1249 if (CALENDAR_ERROR_NONE == calendar_list_get_current_record_p(list, &record)) {
1250 ret = _cal_db_todo_replace_record(record, ids[i]);
1251 if (CALENDAR_ERROR_NONE != ret) {
1252 /* LCOV_EXCL_START */
1253 ERR("_cal_db_todo_replace_record() Fail(%d)", ret);
1254 return CALENDAR_ERROR_DB_FAILED;
1255 /* LCOV_EXCL_STOP */
1258 if (CALENDAR_ERROR_NO_DATA != calendar_list_next(list))
1262 return CALENDAR_ERROR_NONE;
1266 static int _cal_db_todo_get_count_with_query(calendar_query_h query, int *out_count)
1268 cal_query_s *que = NULL;
1269 int ret = CALENDAR_ERROR_NONE;
1270 char *condition = NULL;
1273 GSList *bind_text = NULL;
1275 que = (cal_query_s *)query;
1277 if (CAL_STRING_EQUAL == strcmp(que->view_uri, CALENDAR_VIEW_TODO)) {
1278 table_name = cal_strdup(CAL_VIEW_TABLE_TODO);
1279 } else if (CAL_STRING_EQUAL == strcmp(que->view_uri, CALENDAR_VIEW_TODO_CALENDAR)) {
1280 table_name = cal_strdup(CAL_VIEW_TABLE_TODO_CALENDAR);
1282 /* LCOV_EXCL_START */
1283 ERR("uri(%s) not support get records with query", que->view_uri);
1284 return CALENDAR_ERROR_INVALID_PARAMETER;
1285 /* LCOV_EXCL_STOP */
1290 ret = cal_db_query_create_condition(query, &condition, &bind_text);
1291 if (CALENDAR_ERROR_NONE != ret) {
1292 /* LCOV_EXCL_START */
1293 ERR("cal_db_query_create_condition() Fail(%d), ret");
1294 CAL_FREE(table_name);
1296 /* LCOV_EXCL_STOP */
1300 char *query_str = NULL;
1302 cal_db_append_string(&query_str, "SELECT count(*) FROM");
1303 cal_db_append_string(&query_str, table_name);
1304 CAL_FREE(table_name);
1306 /* query: condition */
1308 cal_db_append_string(&query_str, "WHERE (");
1309 cal_db_append_string(&query_str, condition);
1310 cal_db_append_string(&query_str, ")");
1311 CAL_FREE(condition);
1315 ret = cal_db_util_query_get_first_int_result(query_str, bind_text, &count);
1316 if (CALENDAR_ERROR_NONE != ret) {
1317 /* LCOV_EXCL_START */
1318 ERR("cal_db_util_query_get_first_int_result() Fail");
1320 g_slist_free_full(bind_text, free);
1323 CAL_FREE(query_str);
1325 /* LCOV_EXCL_STOP */
1327 DBG("count(%d) str[%s]", count, query_str);
1329 if (out_count) *out_count = count;
1331 g_slist_free_full(bind_text, free);
1335 CAL_FREE(query_str);
1336 return CALENDAR_ERROR_NONE;
1339 static void _cal_db_todo_get_stmt(sqlite3_stmt *stmt, bool is_view_table, calendar_record_h record, int *extended)
1341 cal_todo_s *todo = NULL;
1342 const unsigned char *temp;
1345 todo = (cal_todo_s*)(record);
1347 todo->index = sqlite3_column_int(stmt, count++);
1348 sqlite3_column_int(stmt, count++);
1350 temp = sqlite3_column_text(stmt, count++);
1351 todo->summary = cal_strdup((const char*)temp);
1352 temp = sqlite3_column_text(stmt, count++);
1353 todo->description = cal_strdup((const char*)temp);
1355 temp = sqlite3_column_text(stmt, count++);
1356 todo->location = cal_strdup((const char*)temp);
1358 temp = sqlite3_column_text(stmt, count++);
1359 todo->categories = cal_strdup((const char*)temp);
1361 sqlite3_column_text(stmt, count++);
1363 todo->todo_status = sqlite3_column_int(stmt, count++);
1364 todo->priority = sqlite3_column_int(stmt, count++);
1365 sqlite3_column_int(stmt, count++);
1366 sqlite3_column_int(stmt, count++);
1367 sqlite3_column_int(stmt, count++);
1368 todo->sensitivity = sqlite3_column_int(stmt, count++);
1370 temp = sqlite3_column_text(stmt, count++);
1371 todo->uid = cal_strdup((const char*)temp);
1373 temp = sqlite3_column_text(stmt, count++);
1374 todo->organizer_name = cal_strdup((const char*)temp);
1376 temp = sqlite3_column_text(stmt, count++);
1377 todo->organizer_email = cal_strdup((const char*)temp);
1379 sqlite3_column_int(stmt, count++);
1381 todo->calendar_id = sqlite3_column_int(stmt, count++);
1383 sqlite3_column_int(stmt, count++);
1385 todo->latitude = sqlite3_column_double(stmt, count++);
1386 todo->longitude = sqlite3_column_double(stmt, count++);
1387 sqlite3_column_int(stmt, count++);
1389 todo->created_time = sqlite3_column_int64(stmt, count++);
1391 todo->completed_time = sqlite3_column_int64(stmt, count++);
1393 todo->progress = sqlite3_column_int(stmt, count++);
1395 sqlite3_column_int(stmt, count++);
1396 sqlite3_column_int(stmt, count++);
1397 todo->is_deleted = sqlite3_column_int(stmt, count++);
1399 todo->start.type = sqlite3_column_int(stmt, count++);
1401 if (todo->start.type == CALENDAR_TIME_UTIME) {
1402 todo->start.time.utime = sqlite3_column_int64(stmt, count++);
1403 count++; /* dtstart_datetime */
1405 count++; /* dtstart_utime */
1406 temp = sqlite3_column_text(stmt, count++);
1408 sscanf((const char *)temp, CAL_FORMAT_LOCAL_DATETIME, &(todo->start.time.date.year),
1409 &(todo->start.time.date.month), &(todo->start.time.date.mday),
1410 &(todo->start.time.date.hour), &(todo->start.time.date.minute),
1411 &(todo->start.time.date.second));
1415 temp = sqlite3_column_text(stmt, count++);
1416 todo->start_tzid = cal_strdup((const char*)temp);
1417 todo->due.type = sqlite3_column_int(stmt, count++);
1418 if (todo->due.type == CALENDAR_TIME_UTIME) {
1419 todo->due.time.utime = sqlite3_column_int64(stmt, count++);
1420 count++; /* datatime */
1423 temp = sqlite3_column_text(stmt, count++);
1425 sscanf((const char *)temp, CAL_FORMAT_LOCAL_DATETIME, &(todo->due.time.date.year),
1426 &(todo->due.time.date.month), &(todo->due.time.date.mday),
1427 &(todo->due.time.date.hour), &(todo->due.time.date.minute),
1428 &(todo->due.time.date.second));
1431 temp = sqlite3_column_text(stmt, count++);
1432 todo->due_tzid = cal_strdup((const char*)temp);
1434 todo->last_mod = sqlite3_column_int64(stmt, count++);
1435 sqlite3_column_int(stmt, count++);
1437 sqlite3_column_text(stmt, count++);
1438 sqlite3_column_text(stmt, count++);
1439 todo->has_attendee = sqlite3_column_int(stmt, count++);
1440 todo->has_alarm = sqlite3_column_int(stmt, count++);
1441 todo->system_type = sqlite3_column_int(stmt, count++);
1442 todo->updated = sqlite3_column_int(stmt, count++);
1443 temp = sqlite3_column_text(stmt, count++);
1444 todo->sync_data1 = cal_strdup((const char*)temp);
1445 temp = sqlite3_column_text(stmt, count++);
1446 todo->sync_data2 = cal_strdup((const char*)temp);
1447 temp = sqlite3_column_text(stmt, count++);
1448 todo->sync_data3 = cal_strdup((const char*)temp);
1449 temp = sqlite3_column_text(stmt, count++);
1450 todo->sync_data4 = cal_strdup((const char*)temp);
1452 sqlite3_column_int(stmt, count++);
1455 *extended = sqlite3_column_int(stmt, count++);
1457 todo->freq = sqlite3_column_int(stmt, count++);
1458 todo->is_allday = sqlite3_column_int(stmt, count++);
1460 if (is_view_table == true) {
1461 if (todo->freq <= 0)
1464 todo->range_type = sqlite3_column_int(stmt, count++);
1465 todo->until.type = sqlite3_column_int(stmt, count++);
1466 todo->until.time.utime = sqlite3_column_int64(stmt, count++);
1468 temp = sqlite3_column_text(stmt, count++);
1470 if (CALENDAR_TIME_LOCALTIME == todo->until.type) {
1471 sscanf((const char *)temp, CAL_FORMAT_LOCAL_DATETIME,
1472 &todo->until.time.date.year,
1473 &todo->until.time.date.month,
1474 &todo->until.time.date.mday,
1475 &todo->until.time.date.hour,
1476 &todo->until.time.date.minute,
1477 &todo->until.time.date.second);
1481 todo->count = sqlite3_column_int(stmt, count++);
1482 todo->interval = sqlite3_column_int(stmt, count++);
1484 temp = sqlite3_column_text(stmt, count++);
1485 todo->bysecond = cal_strdup((const char*)temp);
1487 temp = sqlite3_column_text(stmt, count++);
1488 todo->byminute = cal_strdup((const char*)temp);
1490 temp = sqlite3_column_text(stmt, count++);
1491 todo->byhour = cal_strdup((const char*)temp);
1493 temp = sqlite3_column_text(stmt, count++);
1494 todo->byday = cal_strdup((const char*)temp);
1496 temp = sqlite3_column_text(stmt, count++);
1497 todo->bymonthday = cal_strdup((const char*)temp);
1499 temp = sqlite3_column_text(stmt, count++);
1500 todo->byyearday = cal_strdup((const char*)temp);
1502 temp = sqlite3_column_text(stmt, count++);
1503 todo->byweekno = cal_strdup((const char*)temp);
1505 temp = sqlite3_column_text(stmt, count++);
1506 todo->bymonth = cal_strdup((const char*)temp);
1508 temp = sqlite3_column_text(stmt, count++);
1509 todo->bysetpos = cal_strdup((const char*)temp);
1511 todo->wkst = sqlite3_column_int(stmt, count++);
1513 sqlite3_column_int(stmt, count++);
1517 static void _cal_db_todo_get_property_stmt(sqlite3_stmt *stmt,
1518 unsigned int property, int *stmt_count, calendar_record_h record)
1520 cal_todo_s *todo = NULL;
1521 const unsigned char *temp;
1523 todo = (cal_todo_s*)(record);
1526 case CAL_PROPERTY_TODO_ID:
1527 todo->index = sqlite3_column_int(stmt, *stmt_count);
1529 case CAL_PROPERTY_TODO_CALENDAR_ID:
1530 todo->calendar_id = sqlite3_column_int(stmt, *stmt_count);
1532 case CAL_PROPERTY_TODO_SUMMARY:
1533 temp = sqlite3_column_text(stmt, *stmt_count);
1534 todo->summary = cal_strdup((const char*)temp);
1536 case CAL_PROPERTY_TODO_DESCRIPTION:
1537 temp = sqlite3_column_text(stmt, *stmt_count);
1538 todo->description = cal_strdup((const char*)temp);
1540 case CAL_PROPERTY_TODO_LOCATION:
1541 temp = sqlite3_column_text(stmt, *stmt_count);
1542 todo->location = cal_strdup((const char*)temp);
1544 case CAL_PROPERTY_TODO_CATEGORIES:
1545 temp = sqlite3_column_text(stmt, *stmt_count);
1546 todo->categories = cal_strdup((const char*)temp);
1548 case CAL_PROPERTY_TODO_TODO_STATUS:
1549 todo->todo_status = sqlite3_column_int(stmt, *stmt_count);
1551 case CAL_PROPERTY_TODO_PRIORITY:
1552 todo->priority = sqlite3_column_int(stmt, *stmt_count);
1554 case CAL_PROPERTY_TODO_SENSITIVITY:
1555 todo->sensitivity = sqlite3_column_int(stmt, *stmt_count);
1557 case CAL_PROPERTY_TODO_UID:
1558 temp = sqlite3_column_text(stmt, *stmt_count);
1559 todo->uid = cal_strdup((const char*)temp);
1561 case CAL_PROPERTY_TODO_LATITUDE:
1562 todo->latitude = sqlite3_column_double(stmt, *stmt_count);
1564 case CAL_PROPERTY_TODO_LONGITUDE:
1565 todo->longitude = sqlite3_column_double(stmt, *stmt_count);
1567 case CAL_PROPERTY_TODO_PROGRESS:
1568 todo->progress = sqlite3_column_int(stmt, *stmt_count);
1570 case CAL_PROPERTY_TODO_COMPLETED_TIME:
1571 todo->completed_time = sqlite3_column_int64(stmt, *stmt_count);
1573 case CAL_PROPERTY_TODO_CREATED_TIME:
1574 todo->created_time = sqlite3_column_int64(stmt, *stmt_count);
1576 case CAL_PROPERTY_TODO_LAST_MODIFIED_TIME:
1577 todo->last_mod = sqlite3_column_int64(stmt, *stmt_count);
1579 case CAL_PROPERTY_TODO_IS_DELETED:
1580 todo->is_deleted = sqlite3_column_int(stmt, *stmt_count);
1582 case CAL_PROPERTY_TODO_FREQ:
1583 todo->freq = sqlite3_column_int(stmt, *stmt_count);
1585 case CAL_PROPERTY_TODO_RANGE_TYPE:
1586 todo->range_type = sqlite3_column_int(stmt, *stmt_count);
1588 case CAL_PROPERTY_TODO_UNTIL:
1589 todo->until.type = sqlite3_column_int(stmt, *stmt_count);
1590 if (todo->until.type == CALENDAR_TIME_UTIME) {
1591 *stmt_count = *stmt_count+1;
1592 todo->until.time.utime = sqlite3_column_int64(stmt, *stmt_count);
1593 *stmt_count = *stmt_count+1; /* until_datetime */
1595 *stmt_count = *stmt_count+1;
1596 *stmt_count = *stmt_count+1;
1597 temp = sqlite3_column_text(stmt, *stmt_count);
1599 sscanf((const char *)temp, CAL_FORMAT_LOCAL_DATETIME, &(todo->until.time.date.year),
1600 &(todo->until.time.date.month), &(todo->until.time.date.mday),
1601 &(todo->until.time.date.hour), &(todo->until.time.date.minute),
1602 &(todo->until.time.date.second));
1606 case CAL_PROPERTY_TODO_COUNT:
1607 todo->count = sqlite3_column_int(stmt, *stmt_count);
1609 case CAL_PROPERTY_TODO_INTERVAL:
1610 todo->interval = sqlite3_column_int(stmt, *stmt_count);
1612 case CAL_PROPERTY_TODO_BYSECOND:
1613 temp = sqlite3_column_text(stmt, *stmt_count);
1614 todo->bysecond = cal_strdup((const char*)temp);
1616 case CAL_PROPERTY_TODO_BYMINUTE:
1617 temp = sqlite3_column_text(stmt, *stmt_count);
1618 todo->byminute = cal_strdup((const char*)temp);
1620 case CAL_PROPERTY_TODO_BYHOUR:
1621 temp = sqlite3_column_text(stmt, *stmt_count);
1622 todo->byhour = cal_strdup((const char*)temp);
1624 case CAL_PROPERTY_TODO_BYDAY:
1625 temp = sqlite3_column_text(stmt, *stmt_count);
1626 todo->byday = cal_strdup((const char*)temp);
1628 case CAL_PROPERTY_TODO_BYMONTHDAY:
1629 temp = sqlite3_column_text(stmt, *stmt_count);
1630 todo->bymonthday = cal_strdup((const char*)temp);
1632 case CAL_PROPERTY_TODO_BYYEARDAY:
1633 temp = sqlite3_column_text(stmt, *stmt_count);
1634 todo->byyearday = cal_strdup((const char*)temp);
1636 case CAL_PROPERTY_TODO_BYWEEKNO:
1637 temp = sqlite3_column_text(stmt, *stmt_count);
1638 todo->byweekno = cal_strdup((const char*)temp);
1640 case CAL_PROPERTY_TODO_BYMONTH:
1641 temp = sqlite3_column_text(stmt, *stmt_count);
1642 todo->bymonth = cal_strdup((const char*)temp);
1644 case CAL_PROPERTY_TODO_BYSETPOS:
1645 temp = sqlite3_column_text(stmt, *stmt_count);
1646 todo->bysetpos = cal_strdup((const char*)temp);
1648 case CAL_PROPERTY_TODO_WKST:
1649 todo->wkst = sqlite3_column_int(stmt, *stmt_count);
1651 case CAL_PROPERTY_TODO_HAS_ALARM:
1652 todo->has_alarm = sqlite3_column_int(stmt, *stmt_count);
1654 case CAL_PROPERTY_TODO_SYNC_DATA1:
1655 temp = sqlite3_column_text(stmt, *stmt_count);
1656 todo->sync_data1 = cal_strdup((const char*)temp);
1658 case CAL_PROPERTY_TODO_SYNC_DATA2:
1659 temp = sqlite3_column_text(stmt, *stmt_count);
1660 todo->sync_data2 = cal_strdup((const char*)temp);
1662 case CAL_PROPERTY_TODO_SYNC_DATA3:
1663 temp = sqlite3_column_text(stmt, *stmt_count);
1664 todo->sync_data3 = cal_strdup((const char*)temp);
1666 case CAL_PROPERTY_TODO_SYNC_DATA4:
1667 temp = sqlite3_column_text(stmt, *stmt_count);
1668 todo->sync_data4 = cal_strdup((const char*)temp);
1670 case CAL_PROPERTY_TODO_START:
1671 todo->start.type = sqlite3_column_int(stmt, *stmt_count);
1672 if (todo->start.type == CALENDAR_TIME_UTIME) {
1673 *stmt_count = *stmt_count+1;
1674 todo->start.time.utime = sqlite3_column_int64(stmt, *stmt_count);
1675 *stmt_count = *stmt_count+1; /* datetime */
1677 *stmt_count = *stmt_count+1; /* utime */
1678 *stmt_count = *stmt_count+1;
1679 temp = sqlite3_column_text(stmt, *stmt_count);
1681 sscanf((const char *)temp, CAL_FORMAT_LOCAL_DATETIME, &(todo->start.time.date.year),
1682 &(todo->start.time.date.month), &(todo->start.time.date.mday),
1683 &(todo->start.time.date.hour), &(todo->start.time.date.minute),
1684 &(todo->start.time.date.second));
1688 case CAL_PROPERTY_TODO_START_TZID:
1689 temp = sqlite3_column_text(stmt, *stmt_count);
1690 todo->start_tzid = cal_strdup((const char*)temp);
1692 case CAL_PROPERTY_TODO_DUE:
1693 todo->due.type = sqlite3_column_int(stmt, *stmt_count);
1694 if (todo->due.type == CALENDAR_TIME_UTIME) {
1695 *stmt_count = *stmt_count+1;
1696 todo->due.time.utime = sqlite3_column_int64(stmt, *stmt_count);
1697 *stmt_count = *stmt_count+1; /* datatime */
1699 *stmt_count = *stmt_count+1; /* utime */
1700 *stmt_count = *stmt_count+1;
1701 temp = sqlite3_column_text(stmt, *stmt_count);
1703 sscanf((const char *)temp, CAL_FORMAT_LOCAL_DATETIME, &(todo->due.time.date.year),
1704 &(todo->due.time.date.month), &(todo->due.time.date.mday),
1705 &(todo->due.time.date.hour), &(todo->due.time.date.minute),
1706 &(todo->due.time.date.second));
1710 case CAL_PROPERTY_TODO_DUE_TZID:
1711 temp = sqlite3_column_text(stmt, *stmt_count);
1712 todo->due_tzid = cal_strdup((const char*)temp);
1714 case CAL_PROPERTY_TODO_ORGANIZER_NAME:
1715 temp = sqlite3_column_text(stmt, *stmt_count);
1716 todo->organizer_name = cal_strdup((const char*)temp);
1718 case CAL_PROPERTY_TODO_ORGANIZER_EMAIL:
1719 temp = sqlite3_column_text(stmt, *stmt_count);
1720 todo->organizer_email = cal_strdup((const char*)temp);
1722 case CAL_PROPERTY_TODO_HAS_ATTENDEE:
1723 todo->has_attendee = sqlite3_column_int(stmt, *stmt_count);
1726 sqlite3_column_int(stmt, *stmt_count);
1730 *stmt_count = *stmt_count+1;
1733 static void _cal_db_todo_get_projection_stmt(sqlite3_stmt *stmt,
1734 const unsigned int *projection, const int projection_count,
1735 calendar_record_h record)
1740 for (i = 0; i < projection_count; i++)
1741 _cal_db_todo_get_property_stmt(stmt, projection[i], &stmt_count, record);
1744 static bool _cal_db_todo_check_calendar_book_type(calendar_record_h record)
1748 cal_todo_s *todo = (cal_todo_s *)record;
1749 char query[CAL_DB_SQL_MAX_LEN];
1750 sqlite3_stmt *stmt = NULL;
1752 snprintf(query, sizeof(query), "SELECT store_type FROM %s WHERE id = %d ",
1753 CAL_TABLE_CALENDAR, todo->calendar_id);
1755 ret = cal_db_util_query_prepare(query, &stmt);
1756 if (CALENDAR_ERROR_NONE != ret) {
1757 /* LCOV_EXCL_START */
1758 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
1759 SECURE("query[%s]", query);
1761 /* LCOV_EXCL_STOP */
1764 ret = cal_db_util_stmt_step(stmt);
1765 if (CAL_SQLITE_ROW != ret) {
1766 /* LCOV_EXCL_START */
1767 ERR("cal_db_util_stmt_step() Fail(%d)", ret);
1768 sqlite3_finalize(stmt);
1770 /* LCOV_EXCL_STOP */
1772 store_type = sqlite3_column_int(stmt, 0);
1773 sqlite3_finalize(stmt);
1775 switch (store_type) {
1776 case CALENDAR_BOOK_TYPE_NONE:
1777 case CALENDAR_BOOK_TYPE_TODO:
1780 case CALENDAR_BOOK_TYPE_EVENT:
1789 static int _cal_db_todo_update_dirty(calendar_record_h record)
1792 int ret = CALENDAR_ERROR_NONE;
1793 calendar_record_h original_record;
1795 ret = calendar_record_get_int(record, _calendar_todo.id, &todo_id);
1796 RETV_IF(CALENDAR_ERROR_NONE != ret, ret);
1798 DBG("id=%d", todo_id);
1800 RETV_IF(false == _cal_db_todo_check_calendar_book_type(record), CALENDAR_ERROR_INVALID_PARAMETER);
1802 ret = _cal_db_todo_get_record(todo_id, &original_record);
1803 if (CALENDAR_ERROR_NONE != ret) {
1804 /* LCOV_EXCL_START */
1805 ERR("_cal_db_todo_get_record() Fail(%d)", ret);
1807 /* LCOV_EXCL_STOP */
1810 cal_record_s *_record = NULL;
1811 const cal_property_info_s* property_info = NULL;
1812 int property_info_count = 0;
1815 _record = (cal_record_s *)record;
1817 property_info = cal_view_get_property_info(_record->view_uri, &property_info_count);
1819 for (i = 0; i < property_info_count; i++) {
1820 if (true == cal_record_check_property_flag(record, property_info[i].property_id , CAL_PROPERTY_FLAG_DIRTY)) {
1821 if (CAL_PROPERTY_CHECK_DATA_TYPE(property_info[i].property_id, CAL_PROPERTY_DATA_TYPE_INT) == true) {
1823 ret = calendar_record_get_int(record, property_info[i].property_id, &tmp);
1824 if (CALENDAR_ERROR_NONE != ret)
1826 ret = cal_record_set_int(original_record, property_info[i].property_id, tmp);
1827 if (CALENDAR_ERROR_NONE != ret)
1829 } else if (CAL_PROPERTY_CHECK_DATA_TYPE(property_info[i].property_id, CAL_PROPERTY_DATA_TYPE_STR) == true) {
1831 ret = calendar_record_get_str_p(record, property_info[i].property_id, &tmp);
1832 if (CALENDAR_ERROR_NONE != ret)
1834 ret = cal_record_set_str(original_record, property_info[i].property_id, tmp);
1835 if (CALENDAR_ERROR_NONE != ret)
1837 } else if (CAL_PROPERTY_CHECK_DATA_TYPE(property_info[i].property_id, CAL_PROPERTY_DATA_TYPE_DOUBLE) == true) {
1839 ret = calendar_record_get_double(record, property_info[i].property_id, &tmp);
1840 if (CALENDAR_ERROR_NONE != ret)
1842 ret = cal_record_set_double(original_record, property_info[i].property_id, tmp);
1843 if (CALENDAR_ERROR_NONE != ret)
1845 } else if (CAL_PROPERTY_CHECK_DATA_TYPE(property_info[i].property_id, CAL_PROPERTY_DATA_TYPE_LLI) == true) {
1846 long long int tmp = 0;
1847 ret = calendar_record_get_lli(record, property_info[i].property_id, &tmp);
1848 if (CALENDAR_ERROR_NONE != ret)
1850 ret = cal_record_set_lli(original_record, property_info[i].property_id, tmp);
1851 if (CALENDAR_ERROR_NONE != ret)
1853 } else if (CAL_PROPERTY_CHECK_DATA_TYPE(property_info[i].property_id, CAL_PROPERTY_DATA_TYPE_CALTIME) == true) {
1854 calendar_time_s tmp = {0,};
1855 ret = calendar_record_get_caltime(record, property_info[i].property_id, &tmp);
1856 if (CALENDAR_ERROR_NONE != ret)
1858 ret = cal_record_set_caltime(original_record, property_info[i].property_id, tmp);
1859 if (CALENDAR_ERROR_NONE != ret)
1865 cal_todo_s *tmp = (cal_todo_s *)original_record;
1866 cal_todo_s *tmp_src = (cal_todo_s *)record;
1868 cal_list_clone((calendar_list_h)tmp_src->alarm_list, (calendar_list_h *)&tmp->alarm_list);
1869 cal_list_clone((calendar_list_h)tmp_src->attendee_list, (calendar_list_h *)&tmp->attendee_list);
1870 cal_list_clone((calendar_list_h)tmp_src->extended_list, (calendar_list_h *)&tmp->extended_list);
1872 CAL_RECORD_RESET_COMMON((cal_record_s*)original_record);
1873 ret = _cal_db_todo_update_record(original_record);
1875 calendar_record_destroy(original_record, true);
1880 static int _cal_db_todo_get_deleted_data(int id, int* calendar_book_id, int* created_ver)
1883 char query[CAL_DB_SQL_MAX_LEN];
1884 sqlite3_stmt *stmt = NULL;
1886 snprintf(query, sizeof(query), "SELECT calendar_id, created_ver "
1887 "FROM %s WHERE id = %d ",
1888 CAL_TABLE_SCHEDULE, id);
1890 ret = cal_db_util_query_prepare(query, &stmt);
1891 if (CALENDAR_ERROR_NONE != ret) {
1892 /* LCOV_EXCL_START */
1893 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
1894 SECURE("query[%s]", query);
1896 /* LCOV_EXCL_STOP */
1899 ret = cal_db_util_stmt_step(stmt);
1900 if (CAL_SQLITE_ROW != ret) {
1901 /* LCOV_EXCL_START */
1902 ERR("cal_db_util_stmt_step() Fail(%d)", ret);
1903 sqlite3_finalize(stmt);
1905 /* LCOV_EXCL_STOP */
1908 *calendar_book_id = sqlite3_column_int(stmt, 0);
1909 *created_ver = sqlite3_column_int(stmt, 1);
1910 sqlite3_finalize(stmt);
1912 return CALENDAR_ERROR_NONE;