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.
22 #include "cal_internal.h"
23 #include "cal_typedef.h"
25 #include "cal_record.h"
27 #include "cal_db_util.h"
29 #include "cal_db_query.h"
30 #include "cal_access_control.h"
31 #include "cal_utils.h"
33 static int _cal_db_instance_normal_extended_get_all_records(int offset, int limit, calendar_list_h* out_list);
34 static int _cal_db_instance_normal_extended_get_records_with_query(calendar_query_h query, int offset, int limit, calendar_list_h* out_list);
35 static int _cal_db_instance_normal_extended_get_count(int *out_count);
36 static int _cal_db_instance_normal_extended_get_count_with_query(calendar_query_h query, int *out_count);
40 static void _cal_db_instance_normal_extended_get_stmt(sqlite3_stmt *stmt, calendar_record_h record);
41 static void _cal_db_instance_normal_extended_get_property_stmt(sqlite3_stmt *stmt,
42 unsigned int property, int *stmt_count, calendar_record_h record);
43 static void _cal_db_instance_normal_extended_get_projection_stmt(sqlite3_stmt *stmt,
44 const unsigned int *projection, const int projection_count,
45 calendar_record_h record);
47 cal_db_plugin_cb_s cal_db_instance_normal_extended_plugin_cb = {
48 .is_query_only = false,
49 .insert_record = NULL,
51 .update_record = NULL,
52 .delete_record = NULL,
53 .get_all_records = _cal_db_instance_normal_extended_get_all_records,
54 .get_records_with_query = _cal_db_instance_normal_extended_get_records_with_query,
55 .insert_records = NULL,
56 .update_records = NULL,
57 .delete_records = NULL,
58 .get_count = _cal_db_instance_normal_extended_get_count,
59 .get_count_with_query = _cal_db_instance_normal_extended_get_count_with_query,
60 .replace_record = NULL,
61 .replace_records = NULL
64 static int _cal_db_instance_normal_extended_get_all_records(int offset, int limit, calendar_list_h* out_list)
66 int ret = CALENDAR_ERROR_NONE;
67 char offsetquery[CAL_DB_SQL_MAX_LEN] = {0};
68 char limitquery[CAL_DB_SQL_MAX_LEN] = {0};
69 sqlite3_stmt *stmt = NULL;
71 RETV_IF(NULL == out_list, CALENDAR_ERROR_INVALID_PARAMETER);
73 ret = calendar_list_create(out_list);
74 RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "calendar_list_create() Fail(%d)", ret);
77 snprintf(offsetquery, sizeof(offsetquery), "OFFSET %d", offset);
79 snprintf(limitquery, sizeof(limitquery), "LIMIT %d", limit);
81 char *query_str = NULL;
82 cal_db_append_string(&query_str, "SELECT * FROM");
83 cal_db_append_string(&query_str, CAL_VIEW_TABLE_NORMAL_INSTANCE_EXTENDED);
84 cal_db_append_string(&query_str, limitquery);
85 cal_db_append_string(&query_str, offsetquery);
87 ret = cal_db_util_query_prepare(query_str, &stmt);
88 if (CALENDAR_ERROR_NONE != ret) {
90 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
91 SECURE("query[%s]", query_str);
92 calendar_list_destroy(*out_list, true);
99 while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
100 calendar_record_h record;
101 ret = calendar_record_create(_calendar_instance_utime_calendar_book_extended._uri, &record);
102 if (CALENDAR_ERROR_NONE != ret) {
103 /* LCOV_EXCL_START */
104 calendar_list_destroy(*out_list, true);
106 sqlite3_finalize(stmt);
111 _cal_db_instance_normal_extended_get_stmt(stmt, record);
113 ret = calendar_list_add(*out_list, record);
114 if (CALENDAR_ERROR_NONE != ret) {
115 /* LCOV_EXCL_START */
116 calendar_list_destroy(*out_list, true);
118 calendar_record_destroy(record, true);
119 sqlite3_finalize(stmt);
125 sqlite3_finalize(stmt);
128 return CALENDAR_ERROR_NONE;
131 static int _cal_db_instance_normal_extended_get_records_with_query(calendar_query_h query, int offset, int limit, calendar_list_h* out_list)
133 cal_query_s *que = NULL;
134 int ret = CALENDAR_ERROR_NONE;
135 char *condition = NULL;
136 char *projection = NULL;
137 GSList *bind_text = NULL, *cursor = NULL;
138 sqlite3_stmt *stmt = NULL;
142 que = (cal_query_s *)query;
144 table_name = cal_strdup(CAL_VIEW_TABLE_NORMAL_INSTANCE_EXTENDED);
148 ret = cal_db_query_create_condition(query, &condition, &bind_text);
149 if (CALENDAR_ERROR_NONE != ret) {
150 /* LCOV_EXCL_START */
151 ERR("cal_db_query_create_condition() Fail(%d), ret");
152 CAL_FREE(table_name);
158 /* make: projection */
159 ret = cal_db_query_create_projection(query, &projection);
161 char *query_str = NULL;
162 /* query: projection */
164 cal_db_append_string(&query_str, "SELECT");
165 cal_db_append_string(&query_str, projection);
166 cal_db_append_string(&query_str, "FROM");
167 cal_db_append_string(&query_str, table_name);
168 CAL_FREE(projection);
170 cal_db_append_string(&query_str, "SELECT * FROM");
171 cal_db_append_string(&query_str, table_name);
173 CAL_FREE(table_name);
175 /* query: condition */
177 cal_db_append_string(&query_str, "WHERE (");
178 cal_db_append_string(&query_str, condition);
179 cal_db_append_string(&query_str, ")");
184 ret = cal_db_query_create_order(query, condition, &order);
186 cal_db_append_string(&query_str, order);
192 char buf[CAL_STR_SHORT_LEN32] = {0};
194 snprintf(buf, sizeof(buf), "LIMIT %d", limit);
195 cal_db_append_string(&query_str, buf);
198 snprintf(buf, sizeof(buf), "OFFSET %d", offset);
199 cal_db_append_string(&query_str, buf);
204 ret = cal_db_util_query_prepare(query_str, &stmt);
205 if (CALENDAR_ERROR_NONE != ret) {
206 /* LCOV_EXCL_START */
207 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
208 SECURE("query[%s]", query_str);
210 g_slist_free_full(bind_text, free);
220 g_slist_length(bind_text);
221 for (cursor = bind_text, i = 1; cursor; cursor = cursor->next, i++)
222 cal_db_util_stmt_bind_text(stmt, i, cursor->data);
225 ret = calendar_list_create(out_list);
226 if (CALENDAR_ERROR_NONE != ret) {
227 /* LCOV_EXCL_START */
228 ERR("calendar_list_create() Fail");
230 g_slist_free_full(bind_text, free);
233 sqlite3_finalize(stmt);
239 while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
240 calendar_record_h record;
241 ret = calendar_record_create(que->view_uri, &record);
242 if (CALENDAR_ERROR_NONE != ret) {
243 /* LCOV_EXCL_START */
244 calendar_list_destroy(*out_list, true);
248 g_slist_free_full(bind_text, free);
251 sqlite3_finalize(stmt);
256 if (0 < que->projection_count) {
257 cal_record_set_projection(record,
258 que->projection, que->projection_count, que->property_count);
260 _cal_db_instance_normal_extended_get_projection_stmt(stmt,
261 que->projection, que->projection_count, record);
263 _cal_db_instance_normal_extended_get_stmt(stmt, record);
266 ret = calendar_list_add(*out_list, record);
267 if (CALENDAR_ERROR_NONE != ret) {
268 /* LCOV_EXCL_START */
269 calendar_list_destroy(*out_list, true);
271 calendar_record_destroy(record, true);
274 g_slist_free_full(bind_text, free);
277 sqlite3_finalize(stmt);
285 g_slist_free_full(bind_text, free);
288 sqlite3_finalize(stmt);
291 return CALENDAR_ERROR_NONE;
294 static int _cal_db_instance_normal_extended_get_count(int *out_count)
296 RETV_IF(NULL == out_count, CALENDAR_ERROR_INVALID_PARAMETER);
298 char *query_str = NULL;
299 cal_db_append_string(&query_str, "SELECT count(*) FROM");
300 cal_db_append_string(&query_str, CAL_VIEW_TABLE_NORMAL_INSTANCE_EXTENDED);
304 ret = cal_db_util_query_get_first_int_result(query_str, NULL, &count);
305 if (CALENDAR_ERROR_NONE != ret) {
306 /* LCOV_EXCL_START */
307 ERR("cal_db_util_query_get_first_int_result() Fail");
312 DBG("count(%d) str[%s]", count, query_str);
316 return CALENDAR_ERROR_NONE;
319 static int _cal_db_instance_normal_extended_get_count_with_query(calendar_query_h query, int *out_count)
321 cal_query_s *que = NULL;
322 int ret = CALENDAR_ERROR_NONE;
323 char *condition = NULL;
326 GSList *bind_text = NULL;
328 que = (cal_query_s *)query;
329 table_name = cal_strdup(CAL_VIEW_TABLE_NORMAL_INSTANCE_EXTENDED);
333 ret = cal_db_query_create_condition(query, &condition, &bind_text);
334 if (CALENDAR_ERROR_NONE != ret) {
335 /* LCOV_EXCL_START */
336 ERR("cal_db_query_create_condition() Fail(%d), ret");
337 CAL_FREE(table_name);
343 char *query_str = NULL;
345 cal_db_append_string(&query_str, "SELECT count(*) FROM");
346 cal_db_append_string(&query_str, table_name);
347 CAL_FREE(table_name);
349 /* query: condition */
351 cal_db_append_string(&query_str, "WHERE (");
352 cal_db_append_string(&query_str, condition);
353 cal_db_append_string(&query_str, ")");
358 ret = cal_db_util_query_get_first_int_result(query_str, bind_text, &count);
359 if (CALENDAR_ERROR_NONE != ret) {
360 /* LCOV_EXCL_START */
361 ERR("cal_db_util_query_get_first_int_result() Fail");
363 g_slist_free_full(bind_text, free);
371 DBG("count(%d) str[%s]", count, query_str);
373 if (out_count) *out_count = count;
375 g_slist_free_full(bind_text, free);
380 return CALENDAR_ERROR_NONE;
383 static void _cal_db_instance_normal_extended_get_stmt(sqlite3_stmt *stmt, calendar_record_h record)
385 cal_instance_normal_extended_s* instance = (cal_instance_normal_extended_s*)(record);
386 const unsigned char *temp;
389 instance->event_id = sqlite3_column_int(stmt, count++);
390 instance->start.type = sqlite3_column_int(stmt, count++);
391 instance->start.time.utime = sqlite3_column_int64(stmt, count++);
392 count++; /* datatime */
393 instance->end.type = sqlite3_column_int(stmt, count++);
394 instance->end.time.utime = sqlite3_column_int64(stmt, count++);
395 count++; /* datatime */
397 temp = sqlite3_column_text(stmt, count++);
398 instance->summary = cal_strdup((const char*)temp);
400 temp = sqlite3_column_text(stmt, count++);
401 instance->description = cal_strdup((const char*)temp);
403 temp = sqlite3_column_text(stmt, count++);
404 instance->location = cal_strdup((const char*)temp);
406 instance->busy_status = sqlite3_column_int(stmt, count++);
408 instance->event_status = sqlite3_column_int(stmt, count++);
410 instance->priority = sqlite3_column_int(stmt, count++);
412 instance->sensitivity = sqlite3_column_int(stmt, count++);
414 instance->has_rrule = sqlite3_column_int(stmt, count++);
415 if (0 < instance->has_rrule)
416 instance->has_rrule = 1;
418 instance->latitude = sqlite3_column_double(stmt, count++);
419 instance->longitude = sqlite3_column_double(stmt, count++);
420 instance->has_alarm = sqlite3_column_int(stmt, count++);
421 instance->original_event_id = sqlite3_column_int(stmt, count++);
422 instance->calendar_id = sqlite3_column_int(stmt, count++);
423 instance->last_mod = sqlite3_column_int64(stmt, count++);
425 temp = sqlite3_column_text(stmt, count++);
426 instance->sync_data1 = cal_strdup((const char*)temp);
428 temp = sqlite3_column_text(stmt, count++);
429 instance->organizer_name = cal_strdup((const char*)temp);
431 temp = sqlite3_column_text(stmt, count++);
432 instance->categories = cal_strdup((const char*)temp);
434 instance->has_attendee = sqlite3_column_int(stmt, count++);
436 temp = sqlite3_column_text(stmt, count++);
437 instance->sync_data2 = cal_strdup((const char*)temp);
439 temp = sqlite3_column_text(stmt, count++);
440 instance->sync_data3 = cal_strdup((const char*)temp);
442 temp = sqlite3_column_text(stmt, count++);
443 instance->sync_data4 = cal_strdup((const char*)temp);
448 static void _cal_db_instance_normal_extended_get_property_stmt(sqlite3_stmt *stmt,
449 unsigned int property, int *stmt_count, calendar_record_h record)
451 cal_instance_normal_extended_s* instance = (cal_instance_normal_extended_s*)(record);
452 const unsigned char *temp;
455 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_START:
456 instance->start.type = CALENDAR_TIME_UTIME;
457 *stmt_count = *stmt_count+1;
458 instance->start.time.utime = sqlite3_column_int64(stmt, *stmt_count);
459 *stmt_count = *stmt_count+1;
460 *stmt_count = *stmt_count+1;
462 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_END:
463 instance->end.type = CALENDAR_TIME_UTIME;
464 *stmt_count = *stmt_count+1;
465 instance->end.time.utime = sqlite3_column_int64(stmt, *stmt_count);
466 *stmt_count = *stmt_count+1;
467 *stmt_count = *stmt_count+1;
469 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_SUMMARY:
470 temp = sqlite3_column_text(stmt, *stmt_count);
471 instance->summary = cal_strdup((const char*)temp);
473 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_LOCATION:
474 temp = sqlite3_column_text(stmt, *stmt_count);
475 instance->location = cal_strdup((const char*)temp);
477 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_CALENDAR_ID:
478 instance->calendar_id = sqlite3_column_int(stmt, *stmt_count);
480 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_DESCRIPTION:
481 temp = sqlite3_column_text(stmt, *stmt_count);
482 instance->description = cal_strdup((const char*)temp);
484 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_BUSY_STATUS:
485 instance->busy_status = sqlite3_column_int(stmt, *stmt_count);
487 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_EVENT_STATUS:
488 instance->event_status = sqlite3_column_int(stmt, *stmt_count);
490 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_PRIORITY:
491 instance->priority = sqlite3_column_int(stmt, *stmt_count);
493 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_SENSITIVITY:
494 instance->sensitivity = sqlite3_column_int(stmt, *stmt_count);
496 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_HAS_RRULE:
497 instance->has_rrule = sqlite3_column_int(stmt, *stmt_count);
498 if (0 < instance->has_rrule)
499 instance->has_rrule = 1;
502 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_LATITUDE:
503 instance->latitude = sqlite3_column_double(stmt, *stmt_count);
505 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_LONGITUDE:
506 instance->longitude = sqlite3_column_double(stmt, *stmt_count);
508 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_EVENT_ID:
509 instance->event_id = sqlite3_column_int(stmt, *stmt_count);
511 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_HAS_ALARM:
512 instance->has_alarm = sqlite3_column_int(stmt, *stmt_count);
514 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_ORIGINAL_EVENT_ID:
515 instance->original_event_id = sqlite3_column_int(stmt, *stmt_count);
517 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_LAST_MODIFIED_TIME:
518 instance->last_mod = sqlite3_column_int64(stmt, *stmt_count);
520 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_SYNC_DATA1:
521 temp = sqlite3_column_text(stmt, *stmt_count);
522 instance->sync_data1 = cal_strdup((const char*)temp);
524 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_ORGANIZER_NAME:
525 temp = sqlite3_column_text(stmt, *stmt_count);
526 instance->organizer_name = cal_strdup((const char*)temp);
528 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_CATEGORIES:
529 temp = sqlite3_column_text(stmt, *stmt_count);
530 instance->categories = cal_strdup((const char*)temp);
532 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_HAS_ATTENDEE:
533 instance->has_attendee = sqlite3_column_int(stmt, *stmt_count);
535 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_SYNC_DATA2:
536 temp = sqlite3_column_text(stmt, *stmt_count);
537 instance->sync_data2 = cal_strdup((const char*)temp);
539 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_SYNC_DATA3:
540 temp = sqlite3_column_text(stmt, *stmt_count);
541 instance->sync_data3 = cal_strdup((const char*)temp);
543 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_SYNC_DATA4:
544 temp = sqlite3_column_text(stmt, *stmt_count);
545 instance->sync_data4 = cal_strdup((const char*)temp);
548 sqlite3_column_int(stmt, *stmt_count);
552 *stmt_count = *stmt_count+1;
557 static void _cal_db_instance_normal_extended_get_projection_stmt(sqlite3_stmt *stmt,
558 const unsigned int *projection, const int projection_count,
559 calendar_record_h record)
564 for (i = 0; i < projection_count; i++)
565 _cal_db_instance_normal_extended_get_property_stmt(stmt, projection[i], &stmt_count, record);