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);
53 static void _cal_db_todo_get_stmt(sqlite3_stmt *stmt, bool is_view_table, calendar_record_h record, int *extended);
54 static void _cal_db_todo_get_property_stmt(sqlite3_stmt *stmt,
55 unsigned int property, int *stmt_count, calendar_record_h record);
56 static void _cal_db_todo_get_projection_stmt(sqlite3_stmt *stmt,
57 const unsigned int *projection, const int projection_count,
58 calendar_record_h record);
59 static int _cal_db_todo_update_dirty(calendar_record_h record);
60 static int _cal_db_todo_get_deleted_data(int id, int* calendar_book_id, int* created_ver);
61 static bool _cal_db_todo_check_calendar_book_type(calendar_record_h record);
63 cal_db_plugin_cb_s cal_db_todo_plugin_cb = {
64 .is_query_only = false,
65 .insert_record = _cal_db_todo_insert_record,
66 .get_record = _cal_db_todo_get_record,
67 .update_record = _cal_db_todo_update_record,
68 .delete_record = _cal_db_todo_delete_record,
69 .get_all_records = _cal_db_todo_get_all_records,
70 .get_records_with_query = _cal_db_todo_get_records_with_query,
71 .insert_records = NULL,
72 .update_records = NULL,
73 .delete_records = _cal_db_todo_delete_records,
74 .get_count = _cal_db_todo_get_count,
75 .get_count_with_query = _cal_db_todo_get_count_with_query,
76 .replace_record = _cal_db_todo_replace_record,
77 .replace_records = NULL
80 static int _cal_db_todo_insert_record(calendar_record_h record, int* id)
85 char query[CAL_DB_SQL_MAX_LEN] = {0};
86 char dtstart_datetime[CAL_STR_SHORT_LEN32] = {0};
87 char dtend_datetime[CAL_STR_SHORT_LEN32] = {0};
88 sqlite3_stmt *stmt = NULL;
89 cal_todo_s* todo = (cal_todo_s*)(record);
90 cal_rrule_s *rrule = NULL;
92 int calendar_book_id = 0;
93 calendar_record_h record_calendar = NULL;
96 RETV_IF(NULL == todo, CALENDAR_ERROR_INVALID_PARAMETER);
97 RETV_IF(false == _cal_db_todo_check_calendar_book_type(record), CALENDAR_ERROR_INVALID_PARAMETER);
99 if (cal_access_control_have_write_permission(todo->calendar_id) == false) {
100 /* LCOV_EXCL_START */
101 ERR("cal_access_control_have_write_permission() Fail");
102 return CALENDAR_ERROR_PERMISSION_DENIED;
106 ret = calendar_record_get_int(record,
107 _calendar_todo.calendar_book_id, &calendar_book_id);
108 DBG("calendar_book_id(%d)", calendar_book_id);
110 ret = cal_db_get_record(_calendar_book._uri,
111 calendar_book_id, &record_calendar);
112 RETVM_IF(CALENDAR_ERROR_NONE != ret, CALENDAR_ERROR_INVALID_PARAMETER, "calendar_book_id is invalid");
114 calendar_record_destroy(record_calendar, true);
116 has_alarm = cal_db_alarm_has_alarm(todo->alarm_list);
117 input_ver = cal_db_util_get_next_ver();
119 if (CALENDAR_TIME_LOCALTIME == todo->start.type
120 && (0 == todo->start.time.date.hour)
121 && (0 == todo->start.time.date.minute)
122 && (0 == todo->start.time.date.second)
123 && (0 == todo->due.time.date.hour)
124 && (0 == todo->due.time.date.minute)
125 && (0 == todo->due.time.date.second)) {
129 ret = snprintf(query, sizeof(query),
132 "created_ver, changed_ver, "
133 "summary, description, location, categories, "
134 "task_status, priority, "
137 "latitude, longitude, "
138 "created_time, completed_time, progress, "
139 "dtstart_type, dtstart_utime, dtstart_datetime, dtstart_tzid, "
140 "dtend_type, dtend_utime, dtend_datetime, dtend_tzid, "
141 "last_mod, rrule_id, "
142 "has_alarm, system_type, updated, "
143 "sync_data1, sync_data2, sync_data3, sync_data4, "
144 "organizer_name, organizer_email, "
145 "has_attendee, has_extended, "
155 "strftime('%%s', 'now'), %lld, %d, "
158 "strftime('%%s', 'now'), %d, "
165 CAL_SCH_TYPE_TODO, /*event->cal_type,*/
166 input_ver, input_ver,
167 todo->todo_status, todo->priority,
170 todo->latitude, todo->longitude,
171 todo->completed_time, todo->progress,
172 todo->start.type, todo->start.type == CALENDAR_TIME_UTIME ? todo->start.time.utime : 0,
173 todo->due.type, todo->due.type == CALENDAR_TIME_UTIME ? todo->due.time.utime : 0,
174 0 < todo->freq ? 1 : 0,
178 (todo->attendee_list && 0 < todo->attendee_list->count) ? 1 : 0,
179 (todo->extended_list && 0 < todo->extended_list->count) ? 1 : 0,
180 todo->freq, is_allday);
182 ret = cal_db_util_query_prepare(query, &stmt);
183 if (CALENDAR_ERROR_NONE != ret) {
184 /* LCOV_EXCL_START */
185 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
186 SECURE("query[%s]", query);
194 cal_db_util_stmt_bind_text(stmt, count, todo->summary);
197 if (todo->description)
198 cal_db_util_stmt_bind_text(stmt, count, todo->description);
202 cal_db_util_stmt_bind_text(stmt, count, todo->location);
205 if (todo->categories)
206 cal_db_util_stmt_bind_text(stmt, count, todo->categories);
210 cal_db_util_stmt_bind_text(stmt, count, todo->uid);
213 if (CALENDAR_TIME_LOCALTIME == todo->start.type) {
214 snprintf(dtstart_datetime, sizeof(dtstart_datetime), CAL_FORMAT_LOCAL_DATETIME,
215 todo->start.time.date.year,
216 todo->start.time.date.month,
217 todo->start.time.date.mday,
218 todo->start.time.date.hour,
219 todo->start.time.date.minute,
220 todo->start.time.date.second);
221 cal_db_util_stmt_bind_text(stmt, count, dtstart_datetime);
225 if (todo->start_tzid)
226 cal_db_util_stmt_bind_text(stmt, count, todo->start_tzid);
229 if (CALENDAR_TIME_LOCALTIME == todo->due.type) {
230 snprintf(dtend_datetime, sizeof(dtend_datetime), CAL_FORMAT_LOCAL_DATETIME,
231 todo->due.time.date.year,
232 todo->due.time.date.month,
233 todo->due.time.date.mday,
234 todo->due.time.date.hour,
235 todo->due.time.date.minute,
236 todo->due.time.date.second);
237 cal_db_util_stmt_bind_text(stmt, count, dtend_datetime);
242 cal_db_util_stmt_bind_text(stmt, count, todo->due_tzid);
245 if (todo->sync_data1)
246 cal_db_util_stmt_bind_text(stmt, count, todo->sync_data1);
248 if (todo->sync_data2)
249 cal_db_util_stmt_bind_text(stmt, count, todo->sync_data2);
251 if (todo->sync_data3)
252 cal_db_util_stmt_bind_text(stmt, count, todo->sync_data3);
254 if (todo->sync_data4)
255 cal_db_util_stmt_bind_text(stmt, count, todo->sync_data4);
257 if (todo->organizer_name)
258 cal_db_util_stmt_bind_text(stmt, count, todo->organizer_name);
260 if (todo->organizer_email)
261 cal_db_util_stmt_bind_text(stmt, count, todo->organizer_email);
264 ret = cal_db_util_stmt_step(stmt);
265 if (CALENDAR_ERROR_NONE != ret) {
266 /* LCOV_EXCL_START */
267 ERR("cal_db_util_stmt_step() Fail(%d)", ret);
268 SECURE("query[%s]", query);
269 sqlite3_finalize(stmt);
274 index = cal_db_util_last_insert_id();
275 sqlite3_finalize(stmt);
277 calendar_record_get_int(record, _calendar_todo.id, &tmp);
278 cal_record_set_int(record, _calendar_todo.id, index);
282 cal_db_rrule_get_rrule_from_record(record, &rrule);
283 cal_db_rrule_insert_record(index, rrule);
285 if (todo->alarm_list && 0 < todo->alarm_list->count) {
286 ret = cal_db_alarm_insert_records(todo->alarm_list, index);
287 WARN_IF(CALENDAR_ERROR_NONE != ret, "cal_db_alarm_insert_records() Fail(%x)", ret);
290 if (todo->attendee_list && 0 < todo->attendee_list->count) {
291 ret = cal_db_attendee_insert_records(todo->attendee_list, index);
292 WARN_IF(CALENDAR_ERROR_NONE != ret, "cal_db_attendee_insert_records() Fail(%x)", ret);
295 if (todo->extended_list && 0 < todo->extended_list->count) {
296 DBG("insert extended");
297 ret = cal_db_extended_insert_records(todo->extended_list, index, CALENDAR_RECORD_TYPE_TODO);
298 WARN_IF(CALENDAR_ERROR_NONE != ret, "cal_db_extended_insert_records() Fail(%x)", ret);
304 cal_db_util_notify(CAL_NOTI_TYPE_TODO);
306 cal_record_set_int(record, _calendar_todo.id, tmp);
308 return CALENDAR_ERROR_NONE;
311 static int _cal_db_todo_get_record(int id, calendar_record_h* out_record)
313 char query[CAL_DB_SQL_MAX_LEN];
314 cal_todo_s *todo = NULL;
315 cal_rrule_s *rrule = NULL;
316 sqlite3_stmt *stmt = NULL;
318 calendar_record_h record_calendar;
319 calendar_book_sync_event_type_e sync_event_type = CALENDAR_BOOK_SYNC_EVENT_FOR_ME;
322 ret = calendar_record_create(_calendar_todo._uri, out_record);
323 if (CALENDAR_ERROR_NONE != ret) {
324 /* LCOV_EXCL_START */
325 ERR("calendar_record_create() Fail(%d)", ret);
326 return CALENDAR_ERROR_OUT_OF_MEMORY;
330 todo = (cal_todo_s*)(*out_record);
332 snprintf(query, sizeof(query), "SELECT "CAL_QUERY_SCHEDULE_A_ALL" FROM %s AS A "
333 "WHERE id=%d AND (type = %d OR type = %d) AND calendar_id IN "
334 "(select id from %s where deleted = 0)",
336 id, CALENDAR_BOOK_TYPE_TODO, CALENDAR_BOOK_TYPE_NONE,
338 ret = cal_db_util_query_prepare(query, &stmt);
339 if (CALENDAR_ERROR_NONE != ret) {
340 /* LCOV_EXCL_START */
341 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
342 SECURE("query[%s]", query);
343 calendar_record_destroy(*out_record, true);
349 ret = cal_db_util_stmt_step(stmt);
350 if (CAL_SQLITE_ROW != ret) {
351 /* LCOV_EXCL_START */
352 ERR("cal_db_util_stmt_step() Fail(%d)", ret);
353 sqlite3_finalize(stmt);
354 calendar_record_destroy(*out_record, true);
356 if (CALENDAR_ERROR_NONE == ret)
357 return CALENDAR_ERROR_DB_RECORD_NOT_FOUND;
362 _cal_db_todo_get_stmt(stmt, false, *out_record, &extended);
363 sqlite3_finalize(stmt);
366 ret = cal_db_get_record(_calendar_book._uri, todo->calendar_id, &record_calendar);
367 if (CALENDAR_ERROR_NONE == ret) {
368 ret = calendar_record_get_int(record_calendar,
369 _calendar_book.sync_event, (int *)&sync_event_type);
370 calendar_record_destroy(record_calendar, true);
372 if (todo->is_deleted == 1 && sync_event_type != CALENDAR_BOOK_SYNC_EVENT_FOR_EVERY_AND_REMAIN
374 calendar_record_destroy(*out_record, true);
376 return CALENDAR_ERROR_DB_RECORD_NOT_FOUND;
379 if (CALENDAR_RECURRENCE_NONE != todo->freq) {
380 ret = cal_db_rrule_get_rrule(todo->index, &rrule);
381 if (CALENDAR_ERROR_NONE == ret) {
382 cal_db_rrule_set_rrule_to_record(rrule, *out_record);
387 if (todo->has_alarm == 1)
388 cal_db_alarm_get_records(todo->index, todo->alarm_list);
390 if (todo->has_attendee == 1)
391 cal_db_attendee_get_records(todo->index, todo->attendee_list);
394 cal_db_extended_get_records(todo->index, CALENDAR_RECORD_TYPE_TODO, todo->extended_list);
396 return CALENDAR_ERROR_NONE;
399 static int _cal_db_todo_update_record(calendar_record_h record)
401 char query[CAL_DB_SQL_MAX_LEN] = {0};
402 char dtstart_datetime[CAL_STR_SHORT_LEN32] = {0};
403 char dtend_datetime[CAL_STR_SHORT_LEN32] = {0};
404 sqlite3_stmt *stmt = NULL;
405 cal_todo_s* todo = (cal_todo_s*)(record);
406 cal_rrule_s *rrule = NULL;
410 RETV_IF(NULL == todo, CALENDAR_ERROR_INVALID_PARAMETER);
412 if (cal_access_control_have_write_permission(todo->calendar_id) == false) {
413 /* LCOV_EXCL_START */
414 ERR("cal_access_control_have_write_permission() Fail");
415 return CALENDAR_ERROR_PERMISSION_DENIED;
419 if (todo->common.properties_flags != NULL)
420 return _cal_db_todo_update_dirty(record);
422 has_alarm = cal_db_alarm_has_alarm(todo->alarm_list);
424 if (CALENDAR_TIME_LOCALTIME == todo->start.type
425 && (0 == todo->start.time.date.hour)
426 && (0 == todo->start.time.date.minute)
427 && (0 == todo->start.time.date.second)
428 && (0 == todo->due.time.date.hour)
429 && (0 == todo->due.time.date.minute)
430 && (0 == todo->due.time.date.second)) {
434 snprintf(query, sizeof(query), "UPDATE %s SET "
448 "completed_time = %lld,"
450 "dtstart_type = %d, "
451 "dtstart_utime = %lld, "
452 "dtstart_datetime = ?, "
455 "dtend_utime = %lld, "
456 "dtend_datetime = ?, "
458 "last_mod = strftime('%%s', 'now'), "
466 "organizer_name = ?, "
467 "organizer_email = ?, "
469 "has_extended = %d, "
473 cal_db_util_get_next_ver(),
474 CAL_SCH_TYPE_TODO,/*todo->cal_type,*/
481 todo->completed_time,
484 todo->start.type == CALENDAR_TIME_UTIME ? todo->start.time.utime : 0,
486 todo->due.type == CALENDAR_TIME_UTIME ? todo->due.time.utime : 0,
490 (todo->attendee_list && 0 < todo->attendee_list->count) ? 1 : 0,
491 (todo->extended_list && 0 < todo->extended_list->count) ? 1 : 0,
495 ret = cal_db_util_query_prepare(query, &stmt);
496 if (CALENDAR_ERROR_NONE != ret) {
497 /* LCOV_EXCL_START */
498 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
499 SECURE("query[%s]", query);
507 cal_db_util_stmt_bind_text(stmt, count, todo->summary);
510 if (todo->description)
511 cal_db_util_stmt_bind_text(stmt, count, todo->description);
515 cal_db_util_stmt_bind_text(stmt, count, todo->location);
518 if (todo->categories)
519 cal_db_util_stmt_bind_text(stmt, count, todo->categories);
523 cal_db_util_stmt_bind_text(stmt, count, todo->uid);
526 if (CALENDAR_TIME_LOCALTIME == todo->start.type) {
527 snprintf(dtstart_datetime, sizeof(dtstart_datetime), CAL_FORMAT_LOCAL_DATETIME,
528 todo->start.time.date.year,
529 todo->start.time.date.month,
530 todo->start.time.date.mday,
531 todo->start.time.date.hour,
532 todo->start.time.date.minute,
533 todo->start.time.date.second);
534 cal_db_util_stmt_bind_text(stmt, count, dtstart_datetime);
538 if (todo->start_tzid)
539 cal_db_util_stmt_bind_text(stmt, count, todo->start_tzid);
542 if (CALENDAR_TIME_LOCALTIME == todo->due.type) {
543 snprintf(dtend_datetime, sizeof(dtend_datetime), CAL_FORMAT_LOCAL_DATETIME,
544 todo->due.time.date.year,
545 todo->due.time.date.month,
546 todo->due.time.date.mday,
547 todo->due.time.date.hour,
548 todo->due.time.date.minute,
549 todo->due.time.date.second);
550 cal_db_util_stmt_bind_text(stmt, count, dtend_datetime);
555 cal_db_util_stmt_bind_text(stmt, count, todo->due_tzid);
558 if (todo->sync_data1)
559 cal_db_util_stmt_bind_text(stmt, count, todo->sync_data1);
561 if (todo->sync_data2)
562 cal_db_util_stmt_bind_text(stmt, count, todo->sync_data2);
564 if (todo->sync_data3)
565 cal_db_util_stmt_bind_text(stmt, count, todo->sync_data3);
567 if (todo->sync_data4)
568 cal_db_util_stmt_bind_text(stmt, count, todo->sync_data4);
570 if (todo->organizer_name)
571 cal_db_util_stmt_bind_text(stmt, count, todo->organizer_name);
573 if (todo->organizer_email)
574 cal_db_util_stmt_bind_text(stmt, count, todo->organizer_email);
577 ret = cal_db_util_stmt_step(stmt);
578 sqlite3_finalize(stmt);
579 if (CALENDAR_ERROR_NONE != ret) {
580 /* LCOV_EXCL_START */
581 ERR("cal_db_util_stmt_step() Fail(%d)", ret);
586 cal_db_rrule_get_rrule_from_record(record, &rrule);
587 cal_db_rrule_update_record(todo->index, rrule);
590 cal_db_alarm_delete_with_id(todo->index);
591 cal_db_attendee_delete_with_id(todo->index);
592 cal_db_extended_delete_with_id(todo->index, CALENDAR_RECORD_TYPE_TODO);
594 if (todo->alarm_list && 0 < todo->alarm_list->count) {
595 ret = cal_db_alarm_insert_records(todo->alarm_list, todo->index);
596 WARN_IF(CALENDAR_ERROR_NONE != ret, "cal_db_alarm_insert_records() Fail(%d)", ret);
599 if (todo->attendee_list && 0 < todo->attendee_list->count) {
600 ret = cal_db_attendee_insert_records(todo->attendee_list, todo->index);
601 WARN_IF(CALENDAR_ERROR_NONE != ret, "cal_db_attendee_insert_records() Fail(%d)", ret);
605 if (todo->extended_list && 0 < todo->extended_list->count) {
606 DBG("insert extended");
607 ret = cal_db_extended_insert_records(todo->extended_list, todo->index, CALENDAR_RECORD_TYPE_TODO);
608 WARN_IF(CALENDAR_ERROR_NONE != ret, "cal_db_extended_insert_records() Fail(%d)", ret);
611 cal_db_util_notify(CAL_NOTI_TYPE_TODO);
613 return CALENDAR_ERROR_NONE;
616 static int _cal_db_todo_delete_record(int id)
619 int calendar_book_id = 0;
620 char query[CAL_DB_SQL_MAX_LEN] = {0};
622 calendar_book_sync_event_type_e sync_event_type = CALENDAR_BOOK_SYNC_EVENT_FOR_ME;
624 RETVM_IF(id < 0, CALENDAR_ERROR_INVALID_PARAMETER, "id(%d) < 0", id);
626 ret = _cal_db_todo_get_deleted_data(id, &calendar_book_id, &created_ver);
627 if (CALENDAR_ERROR_NONE != ret) {
628 DBG("_cal_db_event_get_deleted_data() Fail");
632 if (cal_access_control_have_write_permission(calendar_book_id) == false) {
633 /* LCOV_EXCL_START */
634 ERR("cal_access_control_have_write_permission() Fail");
635 return CALENDAR_ERROR_PERMISSION_DENIED;
639 snprintf(query, sizeof(query), "SELECT sync_event FROM %s WHERE id = %d ",
640 CAL_TABLE_CALENDAR, calendar_book_id);
641 ret = cal_db_util_query_get_first_int_result(query, NULL, (int *)&sync_event_type);
642 if (CALENDAR_ERROR_NONE != ret) {
643 /* LCOV_EXCL_START */
644 ERR("cal_db_util_query_get_first_int_result() Fail");
648 DBG("sync_event_type(%d)", sync_event_type);
650 if (sync_event_type == CALENDAR_BOOK_SYNC_EVENT_FOR_EVERY_AND_REMAIN) {
651 DBG("set is_delete");
652 snprintf(query, sizeof(query), "UPDATE %s SET is_deleted = 1, changed_ver = %d, "
653 "last_mod = strftime('%%s','now') WHERE id = %d ",
654 CAL_TABLE_SCHEDULE, cal_db_util_get_next_ver(), id);
656 ret = cal_db_util_query_exec(query);
657 if (CALENDAR_ERROR_NONE != ret) {
658 /* LCOV_EXCL_START */
659 ERR("cal_db_util_query_exec() Fail(%d)", ret);
660 SECURE("[%s]", query);
664 DBG("attendee, alarm and rrule will be deleted by trigger after sync clean");
666 cal_db_util_get_next_ver();
669 snprintf(query, sizeof(query), "DELETE FROM %s WHERE id = %d ", CAL_TABLE_SCHEDULE, id);
670 ret = cal_db_util_query_exec(query);
671 if (CALENDAR_ERROR_NONE != ret) {
672 /* LCOV_EXCL_START */
673 ERR("cal_db_util_query_exec() Fail(%d)", ret);
674 SECURE("[%s]", query);
678 DBG("attendee, alarm and rrule is deleted by trigger");
680 cal_db_util_notify(CAL_NOTI_TYPE_TODO);
682 return CALENDAR_ERROR_NONE;
685 static int _cal_db_todo_replace_record(calendar_record_h record, int id)
687 int ret = CALENDAR_ERROR_NONE;
688 char query[CAL_DB_SQL_MAX_LEN] = {0};
689 char dtstart_datetime[CAL_STR_SHORT_LEN32] = {0};
690 char dtend_datetime[CAL_STR_SHORT_LEN32] = {0};
691 sqlite3_stmt *stmt = NULL;
692 cal_todo_s* todo = (cal_todo_s*)(record);
693 cal_rrule_s *rrule = NULL;
696 RETV_IF(NULL == todo, CALENDAR_ERROR_INVALID_PARAMETER);
699 if (cal_access_control_have_write_permission(todo->calendar_id) == false) {
700 /* LCOV_EXCL_START */
701 ERR("cal_access_control_have_write_permission() Fail");
702 return CALENDAR_ERROR_PERMISSION_DENIED;
706 if (todo->common.properties_flags)
707 return _cal_db_todo_update_dirty(record);
710 if (CALENDAR_TIME_LOCALTIME == todo->start.type
711 && (0 == todo->start.time.date.hour)
712 && (0 == todo->start.time.date.minute)
713 && (0 == todo->start.time.date.second)
714 && (0 == todo->due.time.date.hour)
715 && (0 == todo->due.time.date.minute)
716 && (0 == todo->due.time.date.second)) {
720 has_alarm = cal_db_alarm_has_alarm(todo->alarm_list);
721 snprintf(query, sizeof(query), "UPDATE %s SET "
735 "completed_time = %lld,"
737 "dtstart_type = %d, "
738 "dtstart_utime = %lld, "
739 "dtstart_datetime = ?, "
742 "dtend_utime = %lld, "
743 "dtend_datetime = ?, "
745 "last_mod = strftime('%%s', 'now'), "
753 "organizer_name = ?, "
754 "organizer_email = ?, "
756 "has_extended = %d, "
760 cal_db_util_get_next_ver(),
761 CAL_SCH_TYPE_TODO,/*todo->cal_type,*/
768 todo->completed_time,
771 todo->start.type == CALENDAR_TIME_UTIME ? todo->start.time.utime : 0,
773 todo->due.type == CALENDAR_TIME_UTIME ? todo->due.time.utime : 0,
777 (todo->attendee_list && 0 < todo->attendee_list->count) ? 1 : 0,
778 (todo->extended_list && 0 < todo->extended_list->count) ? 1 : 0,
782 ret = cal_db_util_query_prepare(query, &stmt);
783 if (CALENDAR_ERROR_NONE != ret) {
784 /* LCOV_EXCL_START */
785 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
786 SECURE("query[%s]", query);
794 cal_db_util_stmt_bind_text(stmt, count, todo->summary);
797 if (todo->description)
798 cal_db_util_stmt_bind_text(stmt, count, todo->description);
802 cal_db_util_stmt_bind_text(stmt, count, todo->location);
805 if (todo->categories)
806 cal_db_util_stmt_bind_text(stmt, count, todo->categories);
810 cal_db_util_stmt_bind_text(stmt, count, todo->uid);
813 if (CALENDAR_TIME_LOCALTIME == todo->start.type) {
814 snprintf(dtstart_datetime, sizeof(dtstart_datetime), CAL_FORMAT_LOCAL_DATETIME,
815 todo->start.time.date.year,
816 todo->start.time.date.month,
817 todo->start.time.date.mday,
818 todo->start.time.date.hour,
819 todo->start.time.date.minute,
820 todo->start.time.date.second);
821 cal_db_util_stmt_bind_text(stmt, count, dtstart_datetime);
825 if (todo->start_tzid)
826 cal_db_util_stmt_bind_text(stmt, count, todo->start_tzid);
829 if (CALENDAR_TIME_LOCALTIME == todo->due.type) {
830 snprintf(dtend_datetime, sizeof(dtend_datetime), CAL_FORMAT_LOCAL_DATETIME,
831 todo->due.time.date.year,
832 todo->due.time.date.month,
833 todo->due.time.date.mday,
834 todo->due.time.date.hour,
835 todo->due.time.date.minute,
836 todo->due.time.date.second);
837 cal_db_util_stmt_bind_text(stmt, count, dtend_datetime);
842 cal_db_util_stmt_bind_text(stmt, count, todo->due_tzid);
845 if (todo->sync_data1)
846 cal_db_util_stmt_bind_text(stmt, count, todo->sync_data1);
848 if (todo->sync_data2)
849 cal_db_util_stmt_bind_text(stmt, count, todo->sync_data2);
851 if (todo->sync_data3)
852 cal_db_util_stmt_bind_text(stmt, count, todo->sync_data3);
854 if (todo->sync_data4)
855 cal_db_util_stmt_bind_text(stmt, count, todo->sync_data4);
857 if (todo->organizer_name)
858 cal_db_util_stmt_bind_text(stmt, count, todo->organizer_name);
860 if (todo->organizer_email)
861 cal_db_util_stmt_bind_text(stmt, count, todo->organizer_email);
864 ret = cal_db_util_stmt_step(stmt);
865 sqlite3_finalize(stmt);
866 if (CALENDAR_ERROR_NONE != ret) {
867 /* LCOV_EXCL_START */
868 ERR("cal_db_util_stmt_step() Fail(%d)", ret);
873 cal_db_rrule_get_rrule_from_record(record, &rrule);
874 cal_db_rrule_update_record(id, rrule);
877 cal_db_alarm_delete_with_id(id);
878 cal_db_attendee_delete_with_id(id);
879 cal_db_extended_delete_with_id(id, CALENDAR_RECORD_TYPE_TODO);
881 if (todo->alarm_list && 0 < todo->alarm_list->count) {
882 ret = cal_db_alarm_insert_records(todo->alarm_list, id);
883 WARN_IF(CALENDAR_ERROR_NONE != ret, "cal_db_alarm_insert_records() Fail(%x)", ret);
886 if (todo->attendee_list && 0 < todo->attendee_list->count) {
887 ret = cal_db_attendee_insert_records(todo->attendee_list, id);
888 WARN_IF(CALENDAR_ERROR_NONE != ret, "cal_db_attendee_insert_records() Fail(%x)", ret);
891 if (todo->extended_list && 0 < todo->extended_list->count) {
892 DBG("insert extended");
893 ret = cal_db_extended_insert_records(todo->extended_list, id, CALENDAR_RECORD_TYPE_TODO);
894 WARN_IF(CALENDAR_ERROR_NONE != ret, "cal_db_extended_insert_records() Fail(%d)", ret);
897 cal_db_util_notify(CAL_NOTI_TYPE_TODO);
899 return CALENDAR_ERROR_NONE;
902 static int _cal_db_todo_get_all_records(int offset, int limit, calendar_list_h* out_list)
904 int ret = CALENDAR_ERROR_NONE;
905 char offsetquery[CAL_DB_SQL_MAX_LEN] = {0};
906 char limitquery[CAL_DB_SQL_MAX_LEN] = {0};
907 sqlite3_stmt *stmt = NULL;
909 RETV_IF(NULL == out_list, CALENDAR_ERROR_INVALID_PARAMETER);
911 ret = calendar_list_create(out_list);
912 RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "calendar_list_create() Fail(%d)", ret);
915 snprintf(offsetquery, sizeof(offsetquery), "OFFSET %d", offset);
918 snprintf(limitquery, sizeof(limitquery), "LIMIT %d", limit);
920 char *query_str = NULL;
921 cal_db_append_string(&query_str, "SELECT * FROM");
922 cal_db_append_string(&query_str, CAL_VIEW_TABLE_TODO);
923 cal_db_append_string(&query_str, limitquery);
924 cal_db_append_string(&query_str, offsetquery);
926 ret = cal_db_util_query_prepare(query_str, &stmt);
927 if (CALENDAR_ERROR_NONE != ret) {
928 /* LCOV_EXCL_START */
929 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
930 SECURE("query[%s]", query_str);
931 calendar_list_destroy(*out_list, true);
938 while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
939 calendar_record_h record;
941 ret = calendar_record_create(_calendar_todo._uri, &record);
942 if (CALENDAR_ERROR_NONE != ret) {
943 /* LCOV_EXCL_START */
944 calendar_list_destroy(*out_list, true);
946 sqlite3_finalize(stmt);
951 _cal_db_todo_get_stmt(stmt, true, record, &extended);
954 int has_attendee = 0, has_alarm = 0;
956 cal_todo_s* ptodo = (cal_todo_s*) record;
957 calendar_record_get_int(record, _calendar_todo.id, &record_id);
958 if (CALENDAR_ERROR_NONE == calendar_record_get_int(record, _calendar_todo.has_attendee, &has_attendee)) {
959 if (has_attendee == 1)
960 cal_db_attendee_get_records(record_id, ptodo->attendee_list);
962 if (CALENDAR_ERROR_NONE == calendar_record_get_int(record, _calendar_todo.has_alarm, &has_alarm)) {
964 cal_db_alarm_get_records(record_id, ptodo->alarm_list);
968 cal_db_extended_get_records(record_id, CALENDAR_RECORD_TYPE_TODO, ptodo->extended_list);
970 ret = calendar_list_add(*out_list, record);
971 if (CALENDAR_ERROR_NONE != ret) {
972 /* LCOV_EXCL_START */
973 calendar_list_destroy(*out_list, true);
975 calendar_record_destroy(record, true);
976 sqlite3_finalize(stmt);
982 sqlite3_finalize(stmt);
985 return CALENDAR_ERROR_NONE;
988 static int _cal_db_todo_get_records_with_query(calendar_query_h query, int offset, int limit, calendar_list_h* out_list)
990 cal_query_s *que = NULL;
991 int ret = CALENDAR_ERROR_NONE;
992 char *condition = NULL;
993 char *projection = NULL;
994 GSList *bind_text = NULL, *cursor = NULL;
995 sqlite3_stmt *stmt = NULL;
999 que = (cal_query_s *)query;
1001 if (CAL_STRING_EQUAL == strcmp(que->view_uri, CALENDAR_VIEW_TODO)) {
1002 table_name = cal_strdup(CAL_VIEW_TABLE_TODO);
1003 } else if (CAL_STRING_EQUAL == strcmp(que->view_uri, CALENDAR_VIEW_TODO_BOOK)) {
1004 table_name = cal_strdup(CAL_VIEW_TABLE_TODO_BOOK);
1006 /* LCOV_EXCL_START */
1007 ERR("uri(%s) not support get records with query", que->view_uri);
1008 return CALENDAR_ERROR_INVALID_PARAMETER;
1009 /* LCOV_EXCL_STOP */
1014 ret = cal_db_query_create_condition(query, &condition, &bind_text);
1015 if (CALENDAR_ERROR_NONE != ret) {
1016 /* LCOV_EXCL_START */
1017 ERR("cal_db_query_create_condition() Fail(%d), ret");
1018 CAL_FREE(table_name);
1020 /* LCOV_EXCL_STOP */
1024 /* make: projection */
1025 ret = cal_db_query_create_projection(query, &projection);
1027 char *query_str = NULL;
1029 /* query: projection */
1031 cal_db_append_string(&query_str, "SELECT");
1032 cal_db_append_string(&query_str, projection);
1033 cal_db_append_string(&query_str, "FROM");
1034 cal_db_append_string(&query_str, table_name);
1035 CAL_FREE(projection);
1037 cal_db_append_string(&query_str, "SELECT * FROM");
1038 cal_db_append_string(&query_str, table_name);
1040 CAL_FREE(table_name);
1042 /* query: condition */
1044 cal_db_append_string(&query_str, "WHERE (");
1045 cal_db_append_string(&query_str, condition);
1046 cal_db_append_string(&query_str, ")");
1051 ret = cal_db_query_create_order(query, condition, &order);
1053 cal_db_append_string(&query_str, order);
1056 CAL_FREE(condition);
1059 char buf[CAL_STR_SHORT_LEN32] = {0};
1061 snprintf(buf, sizeof(buf), "LIMIT %d", limit);
1062 cal_db_append_string(&query_str, buf);
1065 snprintf(buf, sizeof(buf), "OFFSET %d", offset);
1066 cal_db_append_string(&query_str, buf);
1071 ret = cal_db_util_query_prepare(query_str, &stmt);
1072 if (CALENDAR_ERROR_NONE != ret) {
1073 /* LCOV_EXCL_START */
1074 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
1075 SECURE("query[%s]", query_str);
1077 g_slist_free_full(bind_text, free);
1082 /* LCOV_EXCL_STOP */
1087 g_slist_length(bind_text);
1088 for (cursor = bind_text, i = 1; cursor; cursor = cursor->next, i++)
1089 cal_db_util_stmt_bind_text(stmt, i, cursor->data);
1092 ret = calendar_list_create(out_list);
1093 if (CALENDAR_ERROR_NONE != ret) {
1094 /* LCOV_EXCL_START */
1095 ERR("calendar_list_create() Fail");
1097 g_slist_free_full(bind_text, free);
1100 sqlite3_finalize(stmt);
1101 CAL_FREE(query_str);
1103 /* LCOV_EXCL_STOP */
1106 while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
1107 calendar_record_h record;
1109 int attendee = 1, alarm = 1;
1110 ret = calendar_record_create(_calendar_todo._uri, &record);
1111 if (CALENDAR_ERROR_NONE != ret) {
1112 /* LCOV_EXCL_START */
1113 calendar_list_destroy(*out_list, true);
1117 g_slist_free_full(bind_text, free);
1120 sqlite3_finalize(stmt);
1121 CAL_FREE(query_str);
1123 /* LCOV_EXCL_STOP */
1125 if (0 < que->projection_count) {
1126 cal_record_set_projection(record,
1127 que->projection, que->projection_count, que->property_count);
1129 _cal_db_todo_get_projection_stmt(stmt,
1130 que->projection, que->projection_count,
1133 cal_todo_s *todo = NULL;
1134 _cal_db_todo_get_stmt(stmt, true, record, &extended);
1135 todo = (cal_todo_s*)(record);
1137 attendee = todo->has_attendee;
1138 alarm = todo->has_alarm;
1143 if (cal_db_query_find_projection_property(query, CAL_PROPERTY_TODO_CALENDAR_ALARM) == true && alarm == 1) {
1144 cal_todo_s* todo = (cal_todo_s*) record;
1145 cal_db_alarm_get_records(todo->index, todo->alarm_list);
1147 if (cal_db_query_find_projection_property(query, CAL_PROPERTY_TODO_CALENDAR_ATTENDEE) == true && attendee == 1) {
1148 cal_todo_s* todo = (cal_todo_s*) record;
1149 cal_db_attendee_get_records(todo->index, todo->attendee_list);
1151 if (cal_db_query_find_projection_property(query, CAL_PROPERTY_TODO_EXTENDED) == true && extended == 1) {
1152 cal_todo_s* todo = (cal_todo_s*) record;
1153 cal_db_extended_get_records(todo->index, CALENDAR_RECORD_TYPE_TODO, todo->extended_list);
1156 ret = calendar_list_add(*out_list, record);
1157 if (CALENDAR_ERROR_NONE != ret) {
1158 /* LCOV_EXCL_START */
1159 calendar_list_destroy(*out_list, true);
1161 calendar_record_destroy(record, true);
1164 g_slist_free_full(bind_text, free);
1167 sqlite3_finalize(stmt);
1168 CAL_FREE(query_str);
1170 /* LCOV_EXCL_STOP */
1175 g_slist_free_full(bind_text, free);
1179 sqlite3_finalize(stmt);
1180 CAL_FREE(query_str);
1182 return CALENDAR_ERROR_NONE;
1185 static int _cal_db_todo_delete_records(int ids[], int count)
1189 for (i = 0; i < count; i++) {
1190 ret = _cal_db_todo_delete_record(ids[i]);
1191 if (CALENDAR_ERROR_NONE != ret) {
1192 /* LCOV_EXCL_START */
1193 ERR("_cal_db_todo_delete_record() Fail(%d)", ret);
1194 return CALENDAR_ERROR_DB_FAILED;
1195 /* LCOV_EXCL_STOP */
1198 return CALENDAR_ERROR_NONE;
1201 static int _cal_db_todo_get_count(int *out_count)
1203 RETV_IF(NULL == out_count, CALENDAR_ERROR_INVALID_PARAMETER);
1205 char *query_str = NULL;
1206 cal_db_append_string(&query_str, "SELECT count(*) FROM");
1207 cal_db_append_string(&query_str, CAL_VIEW_TABLE_TODO);
1211 ret = cal_db_util_query_get_first_int_result(query_str, NULL, &count);
1212 if (CALENDAR_ERROR_NONE != ret) {
1213 /* LCOV_EXCL_START */
1214 ERR("cal_db_util_query_get_first_int_result() Fail");
1215 CAL_FREE(query_str);
1217 /* LCOV_EXCL_STOP */
1219 DBG("count(%d) str[%s]", count, query_str);
1220 CAL_FREE(query_str);
1223 return CALENDAR_ERROR_NONE;
1226 static int _cal_db_todo_get_count_with_query(calendar_query_h query, int *out_count)
1228 cal_query_s *que = NULL;
1229 int ret = CALENDAR_ERROR_NONE;
1230 char *condition = NULL;
1233 GSList *bind_text = NULL;
1235 que = (cal_query_s *)query;
1237 if (CAL_STRING_EQUAL == strcmp(que->view_uri, CALENDAR_VIEW_TODO)) {
1238 table_name = cal_strdup(CAL_VIEW_TABLE_TODO);
1239 } else if (CAL_STRING_EQUAL == strcmp(que->view_uri, CALENDAR_VIEW_TODO_BOOK)) {
1240 table_name = cal_strdup(CAL_VIEW_TABLE_TODO_BOOK);
1242 /* LCOV_EXCL_START */
1243 ERR("uri(%s) not support get records with query", que->view_uri);
1244 return CALENDAR_ERROR_INVALID_PARAMETER;
1245 /* LCOV_EXCL_STOP */
1250 ret = cal_db_query_create_condition(query, &condition, &bind_text);
1251 if (CALENDAR_ERROR_NONE != ret) {
1252 /* LCOV_EXCL_START */
1253 ERR("cal_db_query_create_condition() Fail(%d), ret");
1254 CAL_FREE(table_name);
1256 /* LCOV_EXCL_STOP */
1260 char *query_str = NULL;
1262 cal_db_append_string(&query_str, "SELECT count(*) FROM");
1263 cal_db_append_string(&query_str, table_name);
1264 CAL_FREE(table_name);
1266 /* query: condition */
1268 cal_db_append_string(&query_str, "WHERE (");
1269 cal_db_append_string(&query_str, condition);
1270 cal_db_append_string(&query_str, ")");
1271 CAL_FREE(condition);
1275 ret = cal_db_util_query_get_first_int_result(query_str, bind_text, &count);
1276 if (CALENDAR_ERROR_NONE != ret) {
1277 /* LCOV_EXCL_START */
1278 ERR("cal_db_util_query_get_first_int_result() Fail");
1280 g_slist_free_full(bind_text, free);
1283 CAL_FREE(query_str);
1285 /* LCOV_EXCL_STOP */
1287 DBG("count(%d) str[%s]", count, query_str);
1289 if (out_count) *out_count = count;
1291 g_slist_free_full(bind_text, free);
1295 CAL_FREE(query_str);
1296 return CALENDAR_ERROR_NONE;
1299 static void _cal_db_todo_get_stmt(sqlite3_stmt *stmt, bool is_view_table, calendar_record_h record, int *extended)
1301 cal_todo_s *todo = NULL;
1302 const unsigned char *temp;
1305 todo = (cal_todo_s*)(record);
1307 todo->index = sqlite3_column_int(stmt, count++);
1308 sqlite3_column_int(stmt, count++);
1310 temp = sqlite3_column_text(stmt, count++);
1311 todo->summary = cal_strdup((const char*)temp);
1312 temp = sqlite3_column_text(stmt, count++);
1313 todo->description = cal_strdup((const char*)temp);
1315 temp = sqlite3_column_text(stmt, count++);
1316 todo->location = cal_strdup((const char*)temp);
1318 temp = sqlite3_column_text(stmt, count++);
1319 todo->categories = cal_strdup((const char*)temp);
1321 sqlite3_column_text(stmt, count++);
1323 todo->todo_status = sqlite3_column_int(stmt, count++);
1324 todo->priority = sqlite3_column_int(stmt, count++);
1325 sqlite3_column_int(stmt, count++);
1326 sqlite3_column_int(stmt, count++);
1327 sqlite3_column_int(stmt, count++);
1328 todo->sensitivity = sqlite3_column_int(stmt, count++);
1330 temp = sqlite3_column_text(stmt, count++);
1331 todo->uid = cal_strdup((const char*)temp);
1333 temp = sqlite3_column_text(stmt, count++);
1334 todo->organizer_name = cal_strdup((const char*)temp);
1336 temp = sqlite3_column_text(stmt, count++);
1337 todo->organizer_email = cal_strdup((const char*)temp);
1339 sqlite3_column_int(stmt, count++);
1341 todo->calendar_id = sqlite3_column_int(stmt, count++);
1343 sqlite3_column_int(stmt, count++);
1345 todo->latitude = sqlite3_column_double(stmt, count++);
1346 todo->longitude = sqlite3_column_double(stmt, count++);
1347 sqlite3_column_int(stmt, count++);
1349 todo->created_time = sqlite3_column_int64(stmt, count++);
1351 todo->completed_time = sqlite3_column_int64(stmt, count++);
1353 todo->progress = sqlite3_column_int(stmt, count++);
1355 sqlite3_column_int(stmt, count++);
1356 sqlite3_column_int(stmt, count++);
1357 todo->is_deleted = sqlite3_column_int(stmt, count++);
1359 todo->start.type = sqlite3_column_int(stmt, count++);
1361 if (todo->start.type == CALENDAR_TIME_UTIME) {
1362 todo->start.time.utime = sqlite3_column_int64(stmt, count++);
1363 count++; /* dtstart_datetime */
1365 count++; /* dtstart_utime */
1366 temp = sqlite3_column_text(stmt, count++);
1368 sscanf((const char *)temp, CAL_FORMAT_LOCAL_DATETIME, &(todo->start.time.date.year),
1369 &(todo->start.time.date.month), &(todo->start.time.date.mday),
1370 &(todo->start.time.date.hour), &(todo->start.time.date.minute),
1371 &(todo->start.time.date.second));
1375 temp = sqlite3_column_text(stmt, count++);
1376 todo->start_tzid = cal_strdup((const char*)temp);
1377 todo->due.type = sqlite3_column_int(stmt, count++);
1378 if (todo->due.type == CALENDAR_TIME_UTIME) {
1379 todo->due.time.utime = sqlite3_column_int64(stmt, count++);
1380 count++; /* datatime */
1383 temp = sqlite3_column_text(stmt, count++);
1385 sscanf((const char *)temp, CAL_FORMAT_LOCAL_DATETIME, &(todo->due.time.date.year),
1386 &(todo->due.time.date.month), &(todo->due.time.date.mday),
1387 &(todo->due.time.date.hour), &(todo->due.time.date.minute),
1388 &(todo->due.time.date.second));
1391 temp = sqlite3_column_text(stmt, count++);
1392 todo->due_tzid = cal_strdup((const char*)temp);
1394 todo->last_mod = sqlite3_column_int64(stmt, count++);
1395 sqlite3_column_int(stmt, count++);
1397 sqlite3_column_text(stmt, count++);
1398 sqlite3_column_text(stmt, count++);
1399 todo->has_attendee = sqlite3_column_int(stmt, count++);
1400 todo->has_alarm = sqlite3_column_int(stmt, count++);
1401 todo->system_type = sqlite3_column_int(stmt, count++);
1402 todo->updated = sqlite3_column_int(stmt, count++);
1403 temp = sqlite3_column_text(stmt, count++);
1404 todo->sync_data1 = cal_strdup((const char*)temp);
1405 temp = sqlite3_column_text(stmt, count++);
1406 todo->sync_data2 = cal_strdup((const char*)temp);
1407 temp = sqlite3_column_text(stmt, count++);
1408 todo->sync_data3 = cal_strdup((const char*)temp);
1409 temp = sqlite3_column_text(stmt, count++);
1410 todo->sync_data4 = cal_strdup((const char*)temp);
1412 sqlite3_column_int(stmt, count++);
1415 *extended = sqlite3_column_int(stmt, count++);
1417 todo->freq = sqlite3_column_int(stmt, count++);
1418 todo->is_allday = sqlite3_column_int(stmt, count++);
1420 if (is_view_table == true) {
1421 if (todo->freq <= 0)
1424 todo->range_type = sqlite3_column_int(stmt, count++);
1425 todo->until.type = sqlite3_column_int(stmt, count++);
1426 todo->until.time.utime = sqlite3_column_int64(stmt, count++);
1428 temp = sqlite3_column_text(stmt, count++);
1430 if (CALENDAR_TIME_LOCALTIME == todo->until.type) {
1431 sscanf((const char *)temp, CAL_FORMAT_LOCAL_DATETIME,
1432 &todo->until.time.date.year,
1433 &todo->until.time.date.month,
1434 &todo->until.time.date.mday,
1435 &todo->until.time.date.hour,
1436 &todo->until.time.date.minute,
1437 &todo->until.time.date.second);
1441 todo->count = sqlite3_column_int(stmt, count++);
1442 todo->interval = sqlite3_column_int(stmt, count++);
1444 temp = sqlite3_column_text(stmt, count++);
1445 todo->bysecond = cal_strdup((const char*)temp);
1447 temp = sqlite3_column_text(stmt, count++);
1448 todo->byminute = cal_strdup((const char*)temp);
1450 temp = sqlite3_column_text(stmt, count++);
1451 todo->byhour = cal_strdup((const char*)temp);
1453 temp = sqlite3_column_text(stmt, count++);
1454 todo->byday = cal_strdup((const char*)temp);
1456 temp = sqlite3_column_text(stmt, count++);
1457 todo->bymonthday = cal_strdup((const char*)temp);
1459 temp = sqlite3_column_text(stmt, count++);
1460 todo->byyearday = cal_strdup((const char*)temp);
1462 temp = sqlite3_column_text(stmt, count++);
1463 todo->byweekno = cal_strdup((const char*)temp);
1465 temp = sqlite3_column_text(stmt, count++);
1466 todo->bymonth = cal_strdup((const char*)temp);
1468 temp = sqlite3_column_text(stmt, count++);
1469 todo->bysetpos = cal_strdup((const char*)temp);
1471 todo->wkst = sqlite3_column_int(stmt, count++);
1473 sqlite3_column_int(stmt, count++);
1477 static void _cal_db_todo_get_property_stmt(sqlite3_stmt *stmt,
1478 unsigned int property, int *stmt_count, calendar_record_h record)
1480 cal_todo_s *todo = NULL;
1481 const unsigned char *temp;
1483 todo = (cal_todo_s*)(record);
1486 case CAL_PROPERTY_TODO_ID:
1487 todo->index = sqlite3_column_int(stmt, *stmt_count);
1489 case CAL_PROPERTY_TODO_BOOK_ID:
1490 todo->calendar_id = sqlite3_column_int(stmt, *stmt_count);
1492 case CAL_PROPERTY_TODO_SUMMARY:
1493 temp = sqlite3_column_text(stmt, *stmt_count);
1494 todo->summary = cal_strdup((const char*)temp);
1496 case CAL_PROPERTY_TODO_DESCRIPTION:
1497 temp = sqlite3_column_text(stmt, *stmt_count);
1498 todo->description = cal_strdup((const char*)temp);
1500 case CAL_PROPERTY_TODO_LOCATION:
1501 temp = sqlite3_column_text(stmt, *stmt_count);
1502 todo->location = cal_strdup((const char*)temp);
1504 case CAL_PROPERTY_TODO_CATEGORIES:
1505 temp = sqlite3_column_text(stmt, *stmt_count);
1506 todo->categories = cal_strdup((const char*)temp);
1508 case CAL_PROPERTY_TODO_TODO_STATUS:
1509 todo->todo_status = sqlite3_column_int(stmt, *stmt_count);
1511 case CAL_PROPERTY_TODO_PRIORITY:
1512 todo->priority = sqlite3_column_int(stmt, *stmt_count);
1514 case CAL_PROPERTY_TODO_SENSITIVITY:
1515 todo->sensitivity = sqlite3_column_int(stmt, *stmt_count);
1517 case CAL_PROPERTY_TODO_UID:
1518 temp = sqlite3_column_text(stmt, *stmt_count);
1519 todo->uid = cal_strdup((const char*)temp);
1521 case CAL_PROPERTY_TODO_LATITUDE:
1522 todo->latitude = sqlite3_column_double(stmt, *stmt_count);
1524 case CAL_PROPERTY_TODO_LONGITUDE:
1525 todo->longitude = sqlite3_column_double(stmt, *stmt_count);
1527 case CAL_PROPERTY_TODO_PROGRESS:
1528 todo->progress = sqlite3_column_int(stmt, *stmt_count);
1530 case CAL_PROPERTY_TODO_COMPLETED_TIME:
1531 todo->completed_time = sqlite3_column_int64(stmt, *stmt_count);
1533 case CAL_PROPERTY_TODO_CREATED_TIME:
1534 todo->created_time = sqlite3_column_int64(stmt, *stmt_count);
1536 case CAL_PROPERTY_TODO_LAST_MODIFIED_TIME:
1537 todo->last_mod = sqlite3_column_int64(stmt, *stmt_count);
1539 case CAL_PROPERTY_TODO_IS_DELETED:
1540 todo->is_deleted = sqlite3_column_int(stmt, *stmt_count);
1542 case CAL_PROPERTY_TODO_FREQ:
1543 todo->freq = sqlite3_column_int(stmt, *stmt_count);
1545 case CAL_PROPERTY_TODO_RANGE_TYPE:
1546 todo->range_type = sqlite3_column_int(stmt, *stmt_count);
1548 case CAL_PROPERTY_TODO_UNTIL:
1549 todo->until.type = sqlite3_column_int(stmt, *stmt_count);
1550 if (todo->until.type == CALENDAR_TIME_UTIME) {
1551 *stmt_count = *stmt_count+1;
1552 todo->until.time.utime = sqlite3_column_int64(stmt, *stmt_count);
1553 *stmt_count = *stmt_count+1; /* until_datetime */
1555 *stmt_count = *stmt_count+1;
1556 *stmt_count = *stmt_count+1;
1557 temp = sqlite3_column_text(stmt, *stmt_count);
1559 sscanf((const char *)temp, CAL_FORMAT_LOCAL_DATETIME, &(todo->until.time.date.year),
1560 &(todo->until.time.date.month), &(todo->until.time.date.mday),
1561 &(todo->until.time.date.hour), &(todo->until.time.date.minute),
1562 &(todo->until.time.date.second));
1566 case CAL_PROPERTY_TODO_COUNT:
1567 todo->count = sqlite3_column_int(stmt, *stmt_count);
1569 case CAL_PROPERTY_TODO_INTERVAL:
1570 todo->interval = sqlite3_column_int(stmt, *stmt_count);
1572 case CAL_PROPERTY_TODO_BYSECOND:
1573 temp = sqlite3_column_text(stmt, *stmt_count);
1574 todo->bysecond = cal_strdup((const char*)temp);
1576 case CAL_PROPERTY_TODO_BYMINUTE:
1577 temp = sqlite3_column_text(stmt, *stmt_count);
1578 todo->byminute = cal_strdup((const char*)temp);
1580 case CAL_PROPERTY_TODO_BYHOUR:
1581 temp = sqlite3_column_text(stmt, *stmt_count);
1582 todo->byhour = cal_strdup((const char*)temp);
1584 case CAL_PROPERTY_TODO_BYDAY:
1585 temp = sqlite3_column_text(stmt, *stmt_count);
1586 todo->byday = cal_strdup((const char*)temp);
1588 case CAL_PROPERTY_TODO_BYMONTHDAY:
1589 temp = sqlite3_column_text(stmt, *stmt_count);
1590 todo->bymonthday = cal_strdup((const char*)temp);
1592 case CAL_PROPERTY_TODO_BYYEARDAY:
1593 temp = sqlite3_column_text(stmt, *stmt_count);
1594 todo->byyearday = cal_strdup((const char*)temp);
1596 case CAL_PROPERTY_TODO_BYWEEKNO:
1597 temp = sqlite3_column_text(stmt, *stmt_count);
1598 todo->byweekno = cal_strdup((const char*)temp);
1600 case CAL_PROPERTY_TODO_BYMONTH:
1601 temp = sqlite3_column_text(stmt, *stmt_count);
1602 todo->bymonth = cal_strdup((const char*)temp);
1604 case CAL_PROPERTY_TODO_BYSETPOS:
1605 temp = sqlite3_column_text(stmt, *stmt_count);
1606 todo->bysetpos = cal_strdup((const char*)temp);
1608 case CAL_PROPERTY_TODO_WKST:
1609 todo->wkst = sqlite3_column_int(stmt, *stmt_count);
1611 case CAL_PROPERTY_TODO_HAS_ALARM:
1612 todo->has_alarm = sqlite3_column_int(stmt, *stmt_count);
1614 case CAL_PROPERTY_TODO_SYNC_DATA1:
1615 temp = sqlite3_column_text(stmt, *stmt_count);
1616 todo->sync_data1 = cal_strdup((const char*)temp);
1618 case CAL_PROPERTY_TODO_SYNC_DATA2:
1619 temp = sqlite3_column_text(stmt, *stmt_count);
1620 todo->sync_data2 = cal_strdup((const char*)temp);
1622 case CAL_PROPERTY_TODO_SYNC_DATA3:
1623 temp = sqlite3_column_text(stmt, *stmt_count);
1624 todo->sync_data3 = cal_strdup((const char*)temp);
1626 case CAL_PROPERTY_TODO_SYNC_DATA4:
1627 temp = sqlite3_column_text(stmt, *stmt_count);
1628 todo->sync_data4 = cal_strdup((const char*)temp);
1630 case CAL_PROPERTY_TODO_START:
1631 todo->start.type = sqlite3_column_int(stmt, *stmt_count);
1632 if (todo->start.type == CALENDAR_TIME_UTIME) {
1633 *stmt_count = *stmt_count+1;
1634 todo->start.time.utime = sqlite3_column_int64(stmt, *stmt_count);
1635 *stmt_count = *stmt_count+1; /* datetime */
1637 *stmt_count = *stmt_count+1; /* utime */
1638 *stmt_count = *stmt_count+1;
1639 temp = sqlite3_column_text(stmt, *stmt_count);
1641 sscanf((const char *)temp, CAL_FORMAT_LOCAL_DATETIME, &(todo->start.time.date.year),
1642 &(todo->start.time.date.month), &(todo->start.time.date.mday),
1643 &(todo->start.time.date.hour), &(todo->start.time.date.minute),
1644 &(todo->start.time.date.second));
1648 case CAL_PROPERTY_TODO_START_TZID:
1649 temp = sqlite3_column_text(stmt, *stmt_count);
1650 todo->start_tzid = cal_strdup((const char*)temp);
1652 case CAL_PROPERTY_TODO_DUE:
1653 todo->due.type = sqlite3_column_int(stmt, *stmt_count);
1654 if (todo->due.type == CALENDAR_TIME_UTIME) {
1655 *stmt_count = *stmt_count+1;
1656 todo->due.time.utime = sqlite3_column_int64(stmt, *stmt_count);
1657 *stmt_count = *stmt_count+1; /* datatime */
1659 *stmt_count = *stmt_count+1; /* utime */
1660 *stmt_count = *stmt_count+1;
1661 temp = sqlite3_column_text(stmt, *stmt_count);
1663 sscanf((const char *)temp, CAL_FORMAT_LOCAL_DATETIME, &(todo->due.time.date.year),
1664 &(todo->due.time.date.month), &(todo->due.time.date.mday),
1665 &(todo->due.time.date.hour), &(todo->due.time.date.minute),
1666 &(todo->due.time.date.second));
1670 case CAL_PROPERTY_TODO_DUE_TZID:
1671 temp = sqlite3_column_text(stmt, *stmt_count);
1672 todo->due_tzid = cal_strdup((const char*)temp);
1674 case CAL_PROPERTY_TODO_ORGANIZER_NAME:
1675 temp = sqlite3_column_text(stmt, *stmt_count);
1676 todo->organizer_name = cal_strdup((const char*)temp);
1678 case CAL_PROPERTY_TODO_ORGANIZER_EMAIL:
1679 temp = sqlite3_column_text(stmt, *stmt_count);
1680 todo->organizer_email = cal_strdup((const char*)temp);
1682 case CAL_PROPERTY_TODO_HAS_ATTENDEE:
1683 todo->has_attendee = sqlite3_column_int(stmt, *stmt_count);
1686 sqlite3_column_int(stmt, *stmt_count);
1690 *stmt_count = *stmt_count+1;
1693 static void _cal_db_todo_get_projection_stmt(sqlite3_stmt *stmt,
1694 const unsigned int *projection, const int projection_count,
1695 calendar_record_h record)
1700 for (i = 0; i < projection_count; i++)
1701 _cal_db_todo_get_property_stmt(stmt, projection[i], &stmt_count, record);
1704 static bool _cal_db_todo_check_calendar_book_type(calendar_record_h record)
1708 cal_todo_s *todo = (cal_todo_s *)record;
1709 char query[CAL_DB_SQL_MAX_LEN];
1710 sqlite3_stmt *stmt = NULL;
1712 snprintf(query, sizeof(query), "SELECT store_type FROM %s WHERE id = %d ",
1713 CAL_TABLE_CALENDAR, todo->calendar_id);
1715 ret = cal_db_util_query_prepare(query, &stmt);
1716 if (CALENDAR_ERROR_NONE != ret) {
1717 /* LCOV_EXCL_START */
1718 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
1719 SECURE("query[%s]", query);
1721 /* LCOV_EXCL_STOP */
1724 ret = cal_db_util_stmt_step(stmt);
1725 if (CAL_SQLITE_ROW != ret) {
1726 /* LCOV_EXCL_START */
1727 ERR("cal_db_util_stmt_step() Fail(%d)", ret);
1728 sqlite3_finalize(stmt);
1730 /* LCOV_EXCL_STOP */
1732 store_type = sqlite3_column_int(stmt, 0);
1733 sqlite3_finalize(stmt);
1735 switch (store_type) {
1736 case CALENDAR_BOOK_TYPE_NONE:
1737 case CALENDAR_BOOK_TYPE_TODO:
1740 case CALENDAR_BOOK_TYPE_EVENT:
1749 static int _cal_db_todo_update_dirty(calendar_record_h record)
1752 int ret = CALENDAR_ERROR_NONE;
1753 calendar_record_h original_record;
1755 ret = calendar_record_get_int(record, _calendar_todo.id, &todo_id);
1756 RETV_IF(CALENDAR_ERROR_NONE != ret, ret);
1758 DBG("id=%d", todo_id);
1760 RETV_IF(false == _cal_db_todo_check_calendar_book_type(record), CALENDAR_ERROR_INVALID_PARAMETER);
1762 ret = _cal_db_todo_get_record(todo_id, &original_record);
1763 if (CALENDAR_ERROR_NONE != ret) {
1764 /* LCOV_EXCL_START */
1765 ERR("_cal_db_todo_get_record() Fail(%d)", ret);
1767 /* LCOV_EXCL_STOP */
1770 cal_record_s *_record = NULL;
1771 const cal_property_info_s* property_info = NULL;
1772 int property_info_count = 0;
1775 _record = (cal_record_s *)record;
1777 property_info = cal_view_get_property_info(_record->view_uri, &property_info_count);
1779 for (i = 0; i < property_info_count; i++) {
1780 if (true == cal_record_check_property_flag(record, property_info[i].property_id , CAL_PROPERTY_FLAG_DIRTY)) {
1781 if (CAL_PROPERTY_CHECK_DATA_TYPE(property_info[i].property_id, CAL_PROPERTY_DATA_TYPE_INT) == true) {
1783 ret = calendar_record_get_int(record, property_info[i].property_id, &tmp);
1784 if (CALENDAR_ERROR_NONE != ret)
1786 ret = cal_record_set_int(original_record, property_info[i].property_id, tmp);
1787 if (CALENDAR_ERROR_NONE != ret)
1789 } else if (CAL_PROPERTY_CHECK_DATA_TYPE(property_info[i].property_id, CAL_PROPERTY_DATA_TYPE_STR) == true) {
1791 ret = calendar_record_get_str_p(record, property_info[i].property_id, &tmp);
1792 if (CALENDAR_ERROR_NONE != ret)
1794 ret = cal_record_set_str(original_record, property_info[i].property_id, tmp);
1795 if (CALENDAR_ERROR_NONE != ret)
1797 } else if (CAL_PROPERTY_CHECK_DATA_TYPE(property_info[i].property_id, CAL_PROPERTY_DATA_TYPE_DOUBLE) == true) {
1799 ret = calendar_record_get_double(record, property_info[i].property_id, &tmp);
1800 if (CALENDAR_ERROR_NONE != ret)
1802 ret = cal_record_set_double(original_record, property_info[i].property_id, tmp);
1803 if (CALENDAR_ERROR_NONE != ret)
1805 } else if (CAL_PROPERTY_CHECK_DATA_TYPE(property_info[i].property_id, CAL_PROPERTY_DATA_TYPE_LLI) == true) {
1806 long long int tmp = 0;
1807 ret = calendar_record_get_lli(record, property_info[i].property_id, &tmp);
1808 if (CALENDAR_ERROR_NONE != ret)
1810 ret = cal_record_set_lli(original_record, property_info[i].property_id, tmp);
1811 if (CALENDAR_ERROR_NONE != ret)
1813 } else if (CAL_PROPERTY_CHECK_DATA_TYPE(property_info[i].property_id, CAL_PROPERTY_DATA_TYPE_CALTIME) == true) {
1814 calendar_time_s tmp = {0,};
1815 ret = calendar_record_get_caltime(record, property_info[i].property_id, &tmp);
1816 if (CALENDAR_ERROR_NONE != ret)
1818 ret = cal_record_set_caltime(original_record, property_info[i].property_id, tmp);
1819 if (CALENDAR_ERROR_NONE != ret)
1825 cal_todo_s *tmp = (cal_todo_s *)original_record;
1826 cal_todo_s *tmp_src = (cal_todo_s *)record;
1828 cal_list_clone((calendar_list_h)tmp_src->alarm_list, (calendar_list_h *)&tmp->alarm_list);
1829 cal_list_clone((calendar_list_h)tmp_src->attendee_list, (calendar_list_h *)&tmp->attendee_list);
1830 cal_list_clone((calendar_list_h)tmp_src->extended_list, (calendar_list_h *)&tmp->extended_list);
1832 CAL_RECORD_RESET_COMMON((cal_record_s*)original_record);
1833 ret = _cal_db_todo_update_record(original_record);
1835 calendar_record_destroy(original_record, true);
1840 static int _cal_db_todo_get_deleted_data(int id, int* calendar_book_id, int* created_ver)
1843 char query[CAL_DB_SQL_MAX_LEN];
1844 sqlite3_stmt *stmt = NULL;
1846 snprintf(query, sizeof(query), "SELECT calendar_id, created_ver "
1847 "FROM %s WHERE id = %d ",
1848 CAL_TABLE_SCHEDULE, id);
1850 ret = cal_db_util_query_prepare(query, &stmt);
1851 if (CALENDAR_ERROR_NONE != ret) {
1852 /* LCOV_EXCL_START */
1853 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
1854 SECURE("query[%s]", query);
1856 /* LCOV_EXCL_STOP */
1859 ret = cal_db_util_stmt_step(stmt);
1860 if (CAL_SQLITE_ROW != ret) {
1861 /* LCOV_EXCL_START */
1862 ERR("cal_db_util_stmt_step() Fail(%d)", ret);
1863 sqlite3_finalize(stmt);
1865 /* LCOV_EXCL_STOP */
1868 *calendar_book_id = sqlite3_column_int(stmt, 0);
1869 *created_ver = sqlite3_column_int(stmt, 1);
1870 sqlite3_finalize(stmt);
1872 return CALENDAR_ERROR_NONE;