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.
21 #include "cal_internal.h"
22 #include "cal_typedef.h"
24 #include "cal_record.h"
27 #include "cal_db_util.h"
28 #include "cal_db_query.h"
29 #include "cal_access_control.h"
30 #include "cal_utils.h"
33 #include "cal_server_calendar_delete.h"
35 #include "cal_access_control.h"
40 static int _cal_db_calendar_insert_record(calendar_record_h record, int* id);
41 static int _cal_db_calendar_get_record(int id, calendar_record_h* out_record);
42 static int _cal_db_calendar_update_record(calendar_record_h record);
43 static int _cal_db_calendar_delete_record(int id);
44 static int _cal_db_calendar_get_all_records(int offset, int limit, calendar_list_h* out_list);
45 static int _cal_db_calendar_get_records_with_query(calendar_query_h query, int offset, int limit, calendar_list_h* out_list);
46 static int _cal_db_calendar_delete_records(int ids[], int count);
47 static int _cal_db_calendar_get_count(int *out_count);
48 static int _cal_db_calendar_get_count_with_query(calendar_query_h query, int *out_count);
49 static int _cal_db_calendar_replace_record(calendar_record_h record, int id);
50 static int _cal_db_calendar_replace_records(const calendar_list_h list, int ids[], int count);
55 static void _cal_db_calendar_get_stmt(sqlite3_stmt *stmt, calendar_record_h record);
56 static void _cal_db_calendar_get_property_stmt(sqlite3_stmt *stmt,
57 unsigned int property, int stmt_count, calendar_record_h record);
58 static void _cal_db_calendar_get_projection_stmt(sqlite3_stmt *stmt,
59 const unsigned int *projection, const int projection_count,
60 calendar_record_h record);
61 static int _cal_db_calendar_update_projection(calendar_record_h record);
63 cal_db_plugin_cb_s cal_db_calendar_plugin_cb = {
64 .is_query_only = false,
65 .insert_record = _cal_db_calendar_insert_record,
66 .get_record = _cal_db_calendar_get_record,
67 .update_record = _cal_db_calendar_update_record,
68 .delete_record = _cal_db_calendar_delete_record,
69 .get_all_records = _cal_db_calendar_get_all_records,
70 .get_records_with_query = _cal_db_calendar_get_records_with_query,
71 .insert_records = NULL, /* use insert record with bulk count */
72 .update_records = NULL,
73 .delete_records = _cal_db_calendar_delete_records,
74 .get_count = _cal_db_calendar_get_count,
75 .get_count_with_query = _cal_db_calendar_get_count_with_query,
76 .replace_record = _cal_db_calendar_replace_record,
77 .replace_records = _cal_db_calendar_replace_records
80 static bool _cal_db_calendar_check_value_validation(cal_book_s* calendar)
82 RETVM_IF(NULL == calendar, CALENDAR_ERROR_INVALID_PARAMETER, "calendar is NULL");
84 switch (calendar->store_type) {
85 case CALENDAR_BOOK_TYPE_NONE:
86 case CALENDAR_BOOK_TYPE_EVENT:
87 case CALENDAR_BOOK_TYPE_TODO:
92 ERR("store type is invalid(%d)", calendar->store_type);
99 static int _cal_db_calendar_insert_record(calendar_record_h record, int* id)
105 RETV_IF(NULL == record, CALENDAR_ERROR_INVALID_PARAMETER);
107 cal_book_s *book = (cal_book_s *)(record);
108 if (false == _cal_db_calendar_check_value_validation(book)) {
109 /* LCOV_EXCL_START */
110 ERR("cal_db_calendar_check_value_validation() Fail");
111 return CALENDAR_ERROR_INVALID_PARAMETER;
115 char *client_label = NULL;
116 client_label = cal_access_control_get_label();
118 char query[CAL_DB_SQL_MAX_LEN];
119 snprintf(query, sizeof(query), "INSERT INTO %s (uid, updated, name, description, "
120 "color, location, visibility, sync_event, account_id, store_type, "
121 "sync_data1, sync_data2, sync_data3, sync_data4, mode, owner_label) "
122 "VALUES (?, %d, ?, ?, ?, ?, %d, %d, %d, %d, ?, ?, ?, ?, %d, ?)",
123 CAL_TABLE_CALENDAR, book->updated, book->visibility, book->sync_event,
124 book->account_id, book->store_type, book->mode);
126 sqlite3_stmt *stmt = NULL;
127 ret = cal_db_util_query_prepare(query, &stmt);
128 if (CALENDAR_ERROR_NONE != ret) {
129 /* LCOV_EXCL_START */
130 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
131 SECURE("query[%s]", query);
138 cal_db_util_stmt_bind_text(stmt, 1, book->uid);
140 cal_db_util_stmt_bind_text(stmt, 2, book->name);
141 if (book->description)
142 cal_db_util_stmt_bind_text(stmt, 3, book->description);
144 cal_db_util_stmt_bind_text(stmt, 4, book->color);
146 cal_db_util_stmt_bind_text(stmt, 5, book->location);
147 if (book->sync_data1)
148 cal_db_util_stmt_bind_text(stmt, 6, book->sync_data1);
149 if (book->sync_data2)
150 cal_db_util_stmt_bind_text(stmt, 7, book->sync_data2);
151 if (book->sync_data3)
152 cal_db_util_stmt_bind_text(stmt, 8, book->sync_data3);
153 if (book->sync_data4)
154 cal_db_util_stmt_bind_text(stmt, 9, book->sync_data4);
156 cal_db_util_stmt_bind_text(stmt, 10, client_label);
158 ret = cal_db_util_stmt_step(stmt);
159 if (CALENDAR_ERROR_NONE != ret) {
160 /* LCOV_EXCL_START */
161 ERR("cal_db_util_stmt_step() Fail(%d)", ret);
162 sqlite3_finalize(stmt);
167 sqlite3_finalize(stmt);
171 index = cal_db_util_last_insert_id();
173 cal_access_control_reset();
178 cal_db_util_notify(CAL_NOTI_TYPE_CALENDAR);
180 return CALENDAR_ERROR_NONE;
183 static int _cal_db_calendar_get_record(int id, calendar_record_h* out_record)
185 char query[CAL_DB_SQL_MAX_LEN];
186 sqlite3_stmt *stmt = NULL;
189 ret = calendar_record_create(_calendar_book._uri, out_record);
190 if (CALENDAR_ERROR_NONE != ret) {
191 /* LCOV_EXCL_START */
192 ERR("calendar_record_create() Fail(%d)", ret);
193 return CALENDAR_ERROR_OUT_OF_MEMORY;
197 snprintf(query, sizeof(query), "SELECT * FROM %s WHERE id=%d AND (deleted = 0)",
198 CAL_TABLE_CALENDAR, id);
199 ret = cal_db_util_query_prepare(query, &stmt);
200 if (CALENDAR_ERROR_NONE != ret) {
201 /* LCOV_EXCL_START */
202 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
203 SECURE("query[%s]", query);
204 calendar_record_destroy(*out_record, true);
209 ret = cal_db_util_stmt_step(stmt);
210 if (CAL_SQLITE_ROW != ret) {
211 /* LCOV_EXCL_START */
212 ERR("cal_db_util_stmt_step() Fail(%d)", ret);
213 SECURE("query[%s]", query);
214 sqlite3_finalize(stmt);
215 calendar_record_destroy(*out_record, true);
217 if (CALENDAR_ERROR_NONE == ret)
218 return CALENDAR_ERROR_DB_RECORD_NOT_FOUND;
223 _cal_db_calendar_get_stmt(stmt, *out_record);
224 sqlite3_finalize(stmt);
226 return CALENDAR_ERROR_NONE;
229 static int _cal_db_calendar_update_record(calendar_record_h record)
233 RETV_IF(NULL == record, CALENDAR_ERROR_INVALID_PARAMETER);
235 cal_book_s* book = (cal_book_s*)(record);
236 if (false == _cal_db_calendar_check_value_validation(book)) {
237 /* LCOV_EXCL_START */
238 ERR("cal_db_calendar_check_value_validation() Fail");
239 return CALENDAR_ERROR_INVALID_PARAMETER;
243 if (book->common.properties_flags != NULL)
244 return _cal_db_calendar_update_projection(record);
247 char query[CAL_DB_SQL_MAX_LEN] = {0};
248 snprintf(query, sizeof(query), "UPDATE %s SET "
249 "uid =?, updated = %d, name =?, description =?, color =?, location =?, "
250 "visibility =%d, sync_event =%d, account_id =%d, store_type =%d, "
251 "sync_data1 =?, sync_data2 =?, sync_data3 =?, sync_data4 =?, mode =%d "
253 CAL_TABLE_CALENDAR, book->updated, book->visibility, book->sync_event,
254 book->account_id, book->store_type, book->mode, book->index);
256 sqlite3_stmt *stmt = NULL;
257 ret = cal_db_util_query_prepare(query, &stmt);
258 if (CALENDAR_ERROR_NONE != ret) {
259 /* LCOV_EXCL_START */
260 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
261 SECURE("query[%s]", query);
267 cal_db_util_stmt_bind_text(stmt, 1, book->uid);
269 cal_db_util_stmt_bind_text(stmt, 2, book->name);
270 if (book->description)
271 cal_db_util_stmt_bind_text(stmt, 3, book->description);
273 cal_db_util_stmt_bind_text(stmt, 4, book->color);
275 cal_db_util_stmt_bind_text(stmt, 5, book->location);
276 if (book->sync_data1)
277 cal_db_util_stmt_bind_text(stmt, 6, book->sync_data1);
278 if (book->sync_data2)
279 cal_db_util_stmt_bind_text(stmt, 7, book->sync_data2);
280 if (book->sync_data3)
281 cal_db_util_stmt_bind_text(stmt, 8, book->sync_data3);
282 if (book->sync_data4)
283 cal_db_util_stmt_bind_text(stmt, 9, book->sync_data4);
285 ret = cal_db_util_stmt_step(stmt);
286 if (CALENDAR_ERROR_NONE != ret) {
287 /* LCOV_EXCL_START */
288 ERR("cal_db_util_stmt_step() Fail(%d)", ret);
289 sqlite3_finalize(stmt);
290 SECURE("query[%s]", query);
294 sqlite3_finalize(stmt);
295 cal_db_util_notify(CAL_NOTI_TYPE_CALENDAR);
297 return CALENDAR_ERROR_NONE;
300 static int _cal_db_calendar_delete_record(int id)
302 int ret = CALENDAR_ERROR_NONE;
303 char query[CAL_DB_SQL_MAX_LEN] = {0};
304 int calendar_book_id = -1;
306 snprintf(query, sizeof(query), "SELECT id FROM %s WHERE id = %d",
307 CAL_TABLE_CALENDAR, id);
308 ret = cal_db_util_query_get_first_int_result(query, NULL, &calendar_book_id);
309 if (CALENDAR_ERROR_NONE != ret) {
310 /* LCOV_EXCL_START */
311 ERR("cal_db_util_query_get_first_int_result() Fail(%d)", ret);
318 snprintf(query, sizeof(query), "select count(*) from %s", CAL_TABLE_NORMAL_INSTANCE);
319 ret = cal_db_util_query_get_first_int_result(query, NULL, &count);
320 if (CALENDAR_ERROR_NONE != ret) {
321 /* LCOV_EXCL_START */
322 ERR("cal_db_util_query_get_first_int_result() Fail");
327 snprintf(query, sizeof(query), "select count(*) from %s", CAL_TABLE_ALLDAY_INSTANCE);
328 ret = cal_db_util_query_get_first_int_result(query, NULL, &count2);
329 if (CALENDAR_ERROR_NONE != ret) {
330 /* LCOV_EXCL_START */
331 ERR("cal_db_util_query_get_first_int_result() Fail");
339 snprintf(query, sizeof(query), "UPDATE %s SET deleted = 1 WHERE id = %d", CAL_TABLE_CALENDAR, id);
340 ret = cal_db_util_query_exec(query);
341 RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "cal_db_util_query_exec() Fail(%d)", ret);
342 cal_server_calendar_delete_start();
344 snprintf(query, sizeof(query), "DELETE FROM %s WHERE id = %d", CAL_TABLE_CALENDAR, id);
345 ret = cal_db_util_query_exec(query);
346 RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "cal_db_util_query_exec() Fail(%d)", ret);
348 snprintf(query, sizeof(query), "DELETE FROM %s WHERE calendar_id = %d", CAL_TABLE_SCHEDULE, id);
349 ret = cal_db_util_query_exec(query);
350 RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "cal_db_util_query_exec() Fail(%d)", ret);
353 snprintf(query, sizeof(query), "DELETE FROM %s WHERE calendar_id = %d", CAL_TABLE_DELETED, id);
354 ret = cal_db_util_query_exec(query);
355 RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "cal_db_util_query_exec() Fail(%d)", ret);
357 cal_access_control_reset();
359 cal_db_util_notify(CAL_NOTI_TYPE_CALENDAR);
360 return CALENDAR_ERROR_NONE;
363 static int _cal_db_calendar_replace_record(calendar_record_h record, int id)
367 RETV_IF(NULL == record, CALENDAR_ERROR_INVALID_PARAMETER);
369 cal_book_s *book = (cal_book_s *)(record);
370 if (false == _cal_db_calendar_check_value_validation(book)) {
371 /* LCOV_EXCL_START */
372 ERR("cal_db_calendar_check_value_validation() Fail");
373 return CALENDAR_ERROR_INVALID_PARAMETER;
378 if (book->common.properties_flags)
379 return _cal_db_calendar_update_projection(record);
381 char query[CAL_DB_SQL_MAX_LEN] = {0};
382 snprintf(query, sizeof(query), "UPDATE %s SET "
383 "uid =?, updated =%d, name =?, description =?, color =?, location =?, "
384 "visibility =%d, sync_event =%d, account_id =%d, store_type =%d, "
385 "sync_data1 =?, sync_data2 =?, sync_data3 =?, sync_data4 =?, mode =%d "
387 CAL_TABLE_CALENDAR, book->updated, book->visibility, book->sync_event,
388 book->account_id, book->store_type, book->mode, id);
390 sqlite3_stmt *stmt = NULL;
391 ret = cal_db_util_query_prepare(query, &stmt);
392 if (CALENDAR_ERROR_NONE != ret) {
393 /* LCOV_EXCL_START */
394 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
395 SECURE("query[%s]", query);
401 cal_db_util_stmt_bind_text(stmt, 1, book->uid);
403 cal_db_util_stmt_bind_text(stmt, 2, book->name);
404 if (book->description)
405 cal_db_util_stmt_bind_text(stmt, 3, book->description);
407 cal_db_util_stmt_bind_text(stmt, 4, book->color);
409 cal_db_util_stmt_bind_text(stmt, 5, book->location);
410 if (book->sync_data1)
411 cal_db_util_stmt_bind_text(stmt, 6, book->sync_data1);
412 if (book->sync_data2)
413 cal_db_util_stmt_bind_text(stmt, 7, book->sync_data2);
414 if (book->sync_data3)
415 cal_db_util_stmt_bind_text(stmt, 8, book->sync_data3);
416 if (book->sync_data4)
417 cal_db_util_stmt_bind_text(stmt, 9, book->sync_data4);
419 ret = cal_db_util_stmt_step(stmt);
420 if (CALENDAR_ERROR_NONE != ret) {
421 /* LCOV_EXCL_START */
422 ERR("cal_db_util_stmt_step() Fail(%d)", ret);
423 sqlite3_finalize(stmt);
424 SECURE("query[%s]", query);
428 sqlite3_finalize(stmt);
429 cal_db_util_notify(CAL_NOTI_TYPE_CALENDAR);
431 return CALENDAR_ERROR_NONE;
434 static int _cal_db_calendar_get_all_records(int offset, int limit, calendar_list_h* out_list)
436 int ret = CALENDAR_ERROR_NONE;
437 char offsetquery[CAL_DB_SQL_MAX_LEN] = {0};
438 char limitquery[CAL_DB_SQL_MAX_LEN] = {0};
439 sqlite3_stmt *stmt = NULL;
441 RETV_IF(NULL == out_list, CALENDAR_ERROR_INVALID_PARAMETER);
443 ret = calendar_list_create(out_list);
444 RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "calendar_list_create() Fail(%d)", ret);
447 snprintf(offsetquery, sizeof(offsetquery), "OFFSET %d", offset);
449 snprintf(limitquery, sizeof(limitquery), "LIMIT %d", limit);
451 char *query_str = NULL;
452 cal_db_append_string(&query_str, "SELECT * FROM");
453 cal_db_append_string(&query_str, CAL_TABLE_CALENDAR);
454 cal_db_append_string(&query_str, "WHERE deleted = 0");
455 cal_db_append_string(&query_str, limitquery);
456 cal_db_append_string(&query_str, offsetquery);
458 ret = cal_db_util_query_prepare(query_str, &stmt);
459 if (CALENDAR_ERROR_NONE != ret) {
460 /* LCOV_EXCL_START */
461 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
462 SECURE("query[%s]", query_str);
463 calendar_list_destroy(*out_list, true);
470 while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
471 calendar_record_h record;
472 ret = calendar_record_create(_calendar_book._uri, &record);
473 if (CALENDAR_ERROR_NONE != ret) {
474 /* LCOV_EXCL_START */
475 calendar_list_destroy(*out_list, true);
477 sqlite3_finalize(stmt);
482 _cal_db_calendar_get_stmt(stmt, record);
484 ret = calendar_list_add(*out_list, record);
485 if (CALENDAR_ERROR_NONE != ret) {
486 /* LCOV_EXCL_START */
487 calendar_list_destroy(*out_list, true);
489 calendar_record_destroy(record, true);
490 sqlite3_finalize(stmt);
497 sqlite3_finalize(stmt);
499 return CALENDAR_ERROR_NONE;
502 static int _cal_db_calendar_get_records_with_query(calendar_query_h query, int offset, int limit, calendar_list_h* out_list)
504 cal_query_s *que = NULL;
506 int ret = CALENDAR_ERROR_NONE;
507 char *condition = NULL;
508 GSList *bind_text = NULL, *cursor = NULL;
509 sqlite3_stmt *stmt = NULL;
511 que = (cal_query_s *)query;
515 ret = cal_db_query_create_condition(query, &condition, &bind_text);
516 if (CALENDAR_ERROR_NONE != ret) {
517 /* LCOV_EXCL_START */
518 ERR("cal_db_query_create_condition() Fail(%d), ret");
524 /* make: projection */
525 char *projection = NULL;
526 ret = cal_db_query_create_projection(query, &projection);
528 char *query_str = NULL;
529 /* query: projection */
531 cal_db_append_string(&query_str, "SELECT");
532 cal_db_append_string(&query_str, projection);
533 cal_db_append_string(&query_str, "FROM");
534 cal_db_append_string(&query_str, CAL_TABLE_CALENDAR);
535 CAL_FREE(projection);
537 cal_db_append_string(&query_str, "SELECT * FROM");
538 cal_db_append_string(&query_str, CAL_TABLE_CALENDAR);
541 /* query: condition */
543 cal_db_append_string(&query_str, "WHERE (");
544 cal_db_append_string(&query_str, condition);
545 cal_db_append_string(&query_str, ") AND (deleted = 0)");
550 ret = cal_db_query_create_order(query, condition, &order);
552 cal_db_append_string(&query_str, order);
558 char buf[CAL_STR_SHORT_LEN32] = {0};
560 snprintf(buf, sizeof(buf), "LIMIT %d", limit);
561 cal_db_append_string(&query_str, buf);
564 snprintf(buf, sizeof(buf), "OFFSET %d", offset);
565 cal_db_append_string(&query_str, buf);
570 ret = cal_db_util_query_prepare(query_str, &stmt);
571 SECURE("[TEST]---------query[%s]", query_str);
572 if (CALENDAR_ERROR_NONE != ret) {
573 /* LCOV_EXCL_START */
574 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
575 SECURE("query[%s]", query_str);
577 g_slist_free_full(bind_text, free);
587 g_slist_length(bind_text);
588 for (cursor = bind_text, i = 1; cursor; cursor = cursor->next, i++)
589 cal_db_util_stmt_bind_text(stmt, i, cursor->data);
592 ret = calendar_list_create(out_list);
593 if (CALENDAR_ERROR_NONE != ret) {
594 /* LCOV_EXCL_START */
595 ERR("calendar_list_create() Fail");
597 g_slist_free_full(bind_text, free);
600 sqlite3_finalize(stmt);
606 while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
607 calendar_record_h record;
608 ret = calendar_record_create(_calendar_book._uri, &record);
609 if (CALENDAR_ERROR_NONE != ret) {
610 /* LCOV_EXCL_START */
611 ERR("calendar_record_create() Fail(%d)", ret);
612 calendar_list_destroy(*out_list, true);
616 g_slist_free_full(bind_text, free);
619 sqlite3_finalize(stmt);
625 if (0 < que->projection_count) {
626 cal_record_set_projection(record,
627 que->projection, que->projection_count, que->property_count);
629 _cal_db_calendar_get_projection_stmt(stmt,
630 que->projection, que->projection_count,
633 _cal_db_calendar_get_stmt(stmt, record);
636 ret = calendar_list_add(*out_list, record);
637 if (CALENDAR_ERROR_NONE != ret) {
638 /* LCOV_EXCL_START */
639 ERR("calendar_list_add() Fail(%d)", ret);
640 calendar_list_destroy(*out_list, true);
642 calendar_record_destroy(record, true);
645 g_slist_free_full(bind_text, free);
648 sqlite3_finalize(stmt);
656 g_slist_free_full(bind_text, free);
659 sqlite3_finalize(stmt);
662 return CALENDAR_ERROR_NONE;
665 static int _cal_db_calendar_delete_records(int ids[], int count)
669 for (i = 0; i < count; i++) {
670 ret = _cal_db_calendar_delete_record(ids[i]);
671 if (CALENDAR_ERROR_NONE != ret) {
672 /* LCOV_EXCL_START */
673 ERR("_cal_db_calendar_delete_record() Fail(%d)", ret);
674 return CALENDAR_ERROR_DB_FAILED;
678 return CALENDAR_ERROR_NONE;
681 static int _cal_db_calendar_replace_records(const calendar_list_h list, int ids[], int count)
683 calendar_record_h record;
688 /* LCOV_EXCL_START */
689 ERR("Invalid argument: list is NULL");
690 return CALENDAR_ERROR_INVALID_PARAMETER;
694 ret = calendar_list_first(list);
695 if (CALENDAR_ERROR_NONE != ret) {
696 /* LCOV_EXCL_START */
697 ERR("list first error");
702 for (i = 0; i < count; i++) {
703 if (CALENDAR_ERROR_NONE == calendar_list_get_current_record_p(list, &record)) {
704 ret = _cal_db_calendar_replace_record(record, ids[i]);
705 if (CALENDAR_ERROR_NONE != ret) {
706 /* LCOV_EXCL_START */
707 ERR("_cal_db_calendar_replace_record() Fail(%d)", ret);
712 if (CALENDAR_ERROR_NO_DATA != calendar_list_next(list))
716 return CALENDAR_ERROR_NONE;
719 static int _cal_db_calendar_get_count(int *out_count)
721 RETV_IF(NULL == out_count, CALENDAR_ERROR_INVALID_PARAMETER);
723 char *query_str = NULL;
724 cal_db_append_string(&query_str, "SELECT count(*) FROM");
725 cal_db_append_string(&query_str, CAL_TABLE_CALENDAR);
726 cal_db_append_string(&query_str, "WHERE deleted = 0");
730 ret = cal_db_util_query_get_first_int_result(query_str, NULL, &count);
731 if (CALENDAR_ERROR_NONE != ret) {
732 /* LCOV_EXCL_START */
733 ERR("cal_db_util_query_get_first_int_result() failed");
738 DBG("count(%d) str[%s]", count, query_str);
742 return CALENDAR_ERROR_NONE;
745 static int _cal_db_calendar_get_count_with_query(calendar_query_h query, int *out_count)
747 cal_query_s *que = NULL;
748 int ret = CALENDAR_ERROR_NONE;
749 char *condition = NULL;
752 GSList *bind_text = NULL;
754 que = (cal_query_s *)query;
756 if (CAL_STRING_EQUAL == strcmp(que->view_uri, CALENDAR_VIEW_CALENDAR)) {
757 table_name = cal_strdup(CAL_TABLE_CALENDAR);
759 /* LCOV_EXCL_START */
760 ERR("uri(%s) not support get records with query", que->view_uri);
761 return CALENDAR_ERROR_INVALID_PARAMETER;
767 ret = cal_db_query_create_condition(query, &condition, &bind_text);
768 if (CALENDAR_ERROR_NONE != ret) {
769 /* LCOV_EXCL_START */
770 ERR("cal_db_query_create_condition() Fail(%d), ret");
771 CAL_FREE(table_name);
777 char *query_str = NULL;
779 cal_db_append_string(&query_str, "SELECT count(*) FROM");
780 cal_db_append_string(&query_str, table_name);
781 CAL_FREE(table_name);
783 /* query: condition */
785 cal_db_append_string(&query_str, "WHERE (");
786 cal_db_append_string(&query_str, condition);
787 cal_db_append_string(&query_str, ") AND (deleted = 0)");
792 ret = cal_db_util_query_get_first_int_result(query_str, bind_text, &count);
793 if (CALENDAR_ERROR_NONE != ret) {
794 /* LCOV_EXCL_START */
795 ERR("cal_db_util_query_get_first_int_result() failed");
797 g_slist_free_full(bind_text, free);
804 DBG("count(%d) str[%s]", count, query_str);
810 g_slist_free_full(bind_text, free);
814 return CALENDAR_ERROR_NONE;
817 static void _cal_db_calendar_get_stmt(sqlite3_stmt *stmt, calendar_record_h record)
819 cal_book_s* calendar = (cal_book_s*)(record);
821 const unsigned char *temp;
823 calendar->index = sqlite3_column_int(stmt, count++);
825 temp = sqlite3_column_text(stmt, count++);
826 calendar->uid = cal_strdup((const char*)temp);
828 calendar->updated = sqlite3_column_int(stmt, count++);
830 temp = sqlite3_column_text(stmt, count++);
831 calendar->name = cal_strdup((const char*)temp);
833 temp = sqlite3_column_text(stmt, count++);
834 calendar->description = cal_strdup((const char*)temp);
836 temp = sqlite3_column_text(stmt, count++);
837 calendar->color = cal_strdup((const char*)temp);
839 temp = sqlite3_column_text(stmt, count++);
840 calendar->location = cal_strdup((const char*)temp);
842 calendar->visibility = sqlite3_column_int(stmt, count++);
843 calendar->sync_event = sqlite3_column_int(stmt, count++);
844 calendar->is_deleted = sqlite3_column_int(stmt, count++);
845 calendar->account_id = sqlite3_column_int(stmt, count++);
846 calendar->store_type = sqlite3_column_int(stmt, count++);
848 temp = sqlite3_column_text(stmt, count++);
849 calendar->sync_data1 = cal_strdup((const char*)temp);
850 temp = sqlite3_column_text(stmt, count++);
851 calendar->sync_data2 = cal_strdup((const char*)temp);
852 temp = sqlite3_column_text(stmt, count++);
853 calendar->sync_data3 = cal_strdup((const char*)temp);
854 temp = sqlite3_column_text(stmt, count++);
855 calendar->sync_data4 = cal_strdup((const char*)temp);
858 sqlite3_column_int(stmt, count++);
861 calendar->mode = sqlite3_column_int(stmt, count++);
864 sqlite3_column_text(stmt, count++);
867 static void _cal_db_calendar_get_property_stmt(sqlite3_stmt *stmt,
868 unsigned int property, int stmt_count, calendar_record_h record)
870 cal_book_s* calendar = (cal_book_s*)(record);
871 const unsigned char *temp;
874 case CAL_PROPERTY_CALENDAR_ID:
875 calendar->index = sqlite3_column_int(stmt, stmt_count);
877 case CAL_PROPERTY_CALENDAR_UID:
878 temp = sqlite3_column_text(stmt, stmt_count);
879 calendar->uid = cal_strdup((const char*)temp);
881 case CAL_PROPERTY_CALENDAR_NAME:
882 temp = sqlite3_column_text(stmt, stmt_count);
883 calendar->name = cal_strdup((const char*)temp);
885 case CAL_PROPERTY_CALENDAR_DESCRIPTION:
886 temp = sqlite3_column_text(stmt, stmt_count);
887 calendar->description = cal_strdup((const char*)temp);
889 case CAL_PROPERTY_CALENDAR_COLOR:
890 temp = sqlite3_column_text(stmt, stmt_count);
891 calendar->color = cal_strdup((const char*)temp);
893 case CAL_PROPERTY_CALENDAR_LOCATION:
894 temp = sqlite3_column_text(stmt, stmt_count);
895 calendar->location = cal_strdup((const char*)temp);
897 case CAL_PROPERTY_CALENDAR_VISIBILITY:
898 calendar->visibility = sqlite3_column_int(stmt, stmt_count);
900 case CAL_PROPERTY_CALENDAR_SYNC_EVENT:
901 calendar->sync_event = sqlite3_column_int(stmt, stmt_count);
903 case CAL_PROPERTY_CALENDAR_ACCOUNT_ID:
904 calendar->account_id = sqlite3_column_int(stmt, stmt_count);
906 case CAL_PROPERTY_CALENDAR_STORE_TYPE:
907 calendar->store_type = sqlite3_column_int(stmt, stmt_count);
909 case CAL_PROPERTY_CALENDAR_SYNC_DATA1:
910 temp = sqlite3_column_text(stmt, stmt_count);
911 calendar->sync_data1 = cal_strdup((const char*)temp);
913 case CAL_PROPERTY_CALENDAR_SYNC_DATA2:
914 temp = sqlite3_column_text(stmt, stmt_count);
915 calendar->sync_data1 = cal_strdup((const char*)temp);
917 case CAL_PROPERTY_CALENDAR_SYNC_DATA3:
918 temp = sqlite3_column_text(stmt, stmt_count);
919 calendar->sync_data1 = cal_strdup((const char*)temp);
921 case CAL_PROPERTY_CALENDAR_SYNC_DATA4:
922 temp = sqlite3_column_text(stmt, stmt_count);
923 calendar->sync_data1 = cal_strdup((const char*)temp);
925 case CAL_PROPERTY_CALENDAR_MODE:
926 calendar->mode = sqlite3_column_int(stmt, stmt_count);
935 static void _cal_db_calendar_get_projection_stmt(sqlite3_stmt *stmt,
936 const unsigned int *projection, const int projection_count,
937 calendar_record_h record)
941 for (i = 0; i < projection_count; i++)
942 _cal_db_calendar_get_property_stmt(stmt, projection[i], i, record);
945 static int _cal_db_calendar_update_projection(calendar_record_h record)
947 char query[CAL_DB_SQL_MAX_LEN] = {0};
948 sqlite3_stmt *stmt = NULL;
949 cal_book_s* calendar = (cal_book_s*)(record);
952 GSList *bind_text = NULL;
953 GSList *cursor = NULL;
955 ret = cal_db_query_create_projection_update_set(record, &set, &bind_text);
956 RETV_IF(CALENDAR_ERROR_NONE != ret, ret);
958 snprintf(query, sizeof(query), "UPDATE %s SET %s "
960 CAL_TABLE_CALENDAR, set,
963 ret = cal_db_util_query_prepare(query, &stmt);
964 if (CALENDAR_ERROR_NONE != ret) {
965 /* LCOV_EXCL_START */
966 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
967 SECURE("query[%s]", query);
970 g_slist_free_full(bind_text, free);
980 for (cursor = bind_text, i = 1; cursor; cursor = cursor->next, i++)
981 cal_db_util_stmt_bind_text(stmt, i, cursor->data);
984 ret = cal_db_util_stmt_step(stmt);
985 if (CALENDAR_ERROR_NONE != ret) {
986 /* LCOV_EXCL_START */
987 ERR("cal_db_util_stmt_step() Fail(%d)", ret);
988 SECURE("query[%s]", query);
989 sqlite3_finalize(stmt);
992 g_slist_free_full(bind_text, free);
999 sqlite3_finalize(stmt);
1001 cal_db_util_notify(CAL_NOTI_TYPE_CALENDAR);
1005 g_slist_free_full(bind_text, free);
1009 return CALENDAR_ERROR_NONE;