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) {
89 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
90 SECURE("query[%s]", query_str);
91 calendar_list_destroy(*out_list, true);
97 while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
98 calendar_record_h record;
99 ret = calendar_record_create(_calendar_instance_utime_calendar_book_extended._uri, &record);
100 if (CALENDAR_ERROR_NONE != ret) {
101 calendar_list_destroy(*out_list, true);
103 sqlite3_finalize(stmt);
107 _cal_db_instance_normal_extended_get_stmt(stmt, record);
109 ret = calendar_list_add(*out_list, record);
110 if (CALENDAR_ERROR_NONE != ret) {
111 calendar_list_destroy(*out_list, true);
113 calendar_record_destroy(record, true);
114 sqlite3_finalize(stmt);
119 sqlite3_finalize(stmt);
122 return CALENDAR_ERROR_NONE;
125 static int _cal_db_instance_normal_extended_get_records_with_query(calendar_query_h query, int offset, int limit, calendar_list_h* out_list)
127 cal_query_s *que = NULL;
128 int ret = CALENDAR_ERROR_NONE;
129 char *condition = NULL;
130 char *projection = NULL;
131 GSList *bind_text = NULL, *cursor = NULL;
132 sqlite3_stmt *stmt = NULL;
136 que = (cal_query_s *)query;
138 table_name = cal_strdup(CAL_VIEW_TABLE_NORMAL_INSTANCE_EXTENDED);
142 ret = cal_db_query_create_condition(query, &condition, &bind_text);
143 if (CALENDAR_ERROR_NONE != ret) {
144 CAL_FREE(table_name);
145 ERR("cal_db_query_create_condition() Fail(%d), ret");
150 /* make: projection */
151 ret = cal_db_query_create_projection(query, &projection);
153 char *query_str = NULL;
154 /* query: projection */
156 cal_db_append_string(&query_str, "SELECT");
157 cal_db_append_string(&query_str, projection);
158 cal_db_append_string(&query_str, "FROM");
159 cal_db_append_string(&query_str, table_name);
160 CAL_FREE(projection);
162 cal_db_append_string(&query_str, "SELECT * FROM");
163 cal_db_append_string(&query_str, table_name);
165 CAL_FREE(table_name);
167 /* query: condition */
169 cal_db_append_string(&query_str, "WHERE (");
170 cal_db_append_string(&query_str, condition);
171 cal_db_append_string(&query_str, ")");
176 ret = cal_db_query_create_order(query, condition, &order);
178 cal_db_append_string(&query_str, order);
184 char buf[CAL_STR_SHORT_LEN32] = {0};
186 snprintf(buf, sizeof(buf), "LIMIT %d", limit);
187 cal_db_append_string(&query_str, buf);
190 snprintf(buf, sizeof(buf), "OFFSET %d", offset);
191 cal_db_append_string(&query_str, buf);
196 ret = cal_db_util_query_prepare(query_str, &stmt);
197 if (CALENDAR_ERROR_NONE != ret) {
198 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
199 SECURE("query[%s]", query_str);
201 g_slist_free_full(bind_text, free);
210 g_slist_length(bind_text);
211 for (cursor = bind_text, i = 1; cursor; cursor = cursor->next, i++)
212 cal_db_util_stmt_bind_text(stmt, i, cursor->data);
215 ret = calendar_list_create(out_list);
216 if (CALENDAR_ERROR_NONE != ret) {
218 g_slist_free_full(bind_text, free);
221 ERR("calendar_list_create() Fail");
222 sqlite3_finalize(stmt);
227 while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
228 calendar_record_h record;
229 ret = calendar_record_create(que->view_uri, &record);
230 if (CALENDAR_ERROR_NONE != ret) {
231 calendar_list_destroy(*out_list, true);
235 g_slist_free_full(bind_text, free);
238 sqlite3_finalize(stmt);
242 if (0 < que->projection_count) {
243 cal_record_set_projection(record,
244 que->projection, que->projection_count, que->property_count);
246 _cal_db_instance_normal_extended_get_projection_stmt(stmt,
247 que->projection, que->projection_count, record);
249 _cal_db_instance_normal_extended_get_stmt(stmt, record);
252 ret = calendar_list_add(*out_list, record);
253 if (CALENDAR_ERROR_NONE != ret) {
254 calendar_list_destroy(*out_list, true);
256 calendar_record_destroy(record, true);
259 g_slist_free_full(bind_text, free);
262 sqlite3_finalize(stmt);
269 g_slist_free_full(bind_text, free);
272 sqlite3_finalize(stmt);
275 return CALENDAR_ERROR_NONE;
278 static int _cal_db_instance_normal_extended_get_count(int *out_count)
280 RETV_IF(NULL == out_count, CALENDAR_ERROR_INVALID_PARAMETER);
282 char *query_str = NULL;
283 cal_db_append_string(&query_str, "SELECT count(*) FROM");
284 cal_db_append_string(&query_str, CAL_VIEW_TABLE_NORMAL_INSTANCE_EXTENDED);
288 ret = cal_db_util_query_get_first_int_result(query_str, NULL, &count);
289 if (CALENDAR_ERROR_NONE != ret) {
290 ERR("cal_db_util_query_get_first_int_result() Fail");
294 DBG("count(%d) str[%s]", count, query_str);
298 return CALENDAR_ERROR_NONE;
301 static int _cal_db_instance_normal_extended_get_count_with_query(calendar_query_h query, int *out_count)
303 cal_query_s *que = NULL;
304 int ret = CALENDAR_ERROR_NONE;
305 char *condition = NULL;
308 GSList *bind_text = NULL;
310 que = (cal_query_s *)query;
311 table_name = cal_strdup(CAL_VIEW_TABLE_NORMAL_INSTANCE_EXTENDED);
315 ret = cal_db_query_create_condition(query, &condition, &bind_text);
316 if (CALENDAR_ERROR_NONE != ret) {
317 CAL_FREE(table_name);
318 ERR("cal_db_query_create_condition() Fail(%d), ret");
323 char *query_str = NULL;
325 cal_db_append_string(&query_str, "SELECT count(*) FROM");
326 cal_db_append_string(&query_str, table_name);
327 CAL_FREE(table_name);
329 /* query: condition */
331 cal_db_append_string(&query_str, "WHERE (");
332 cal_db_append_string(&query_str, condition);
333 cal_db_append_string(&query_str, ")");
338 ret = cal_db_util_query_get_first_int_result(query_str, bind_text, &count);
339 if (CALENDAR_ERROR_NONE != ret) {
340 ERR("cal_db_util_query_get_first_int_result() Fail");
342 g_slist_free_full(bind_text, free);
349 DBG("count(%d) str[%s]", count, query_str);
351 if (out_count) *out_count = count;
353 g_slist_free_full(bind_text, free);
358 return CALENDAR_ERROR_NONE;
361 static void _cal_db_instance_normal_extended_get_stmt(sqlite3_stmt *stmt, calendar_record_h record)
363 cal_instance_normal_extended_s* instance = (cal_instance_normal_extended_s*)(record);
364 const unsigned char *temp;
367 instance->event_id = sqlite3_column_int(stmt, count++);
368 instance->start.type = sqlite3_column_int(stmt, count++);
369 instance->start.time.utime = sqlite3_column_int64(stmt, count++);
370 count++; /* datatime */
371 instance->end.type = sqlite3_column_int(stmt, count++);
372 instance->end.time.utime = sqlite3_column_int64(stmt, count++);
373 count++; /* datatime */
375 temp = sqlite3_column_text(stmt, count++);
376 instance->summary = cal_strdup((const char*)temp);
378 temp = sqlite3_column_text(stmt, count++);
379 instance->description = cal_strdup((const char*)temp);
381 temp = sqlite3_column_text(stmt, count++);
382 instance->location = cal_strdup((const char*)temp);
384 instance->busy_status = sqlite3_column_int(stmt, count++);
386 instance->event_status = sqlite3_column_int(stmt, count++);
388 instance->priority = sqlite3_column_int(stmt, count++);
390 instance->sensitivity = sqlite3_column_int(stmt, count++);
392 instance->has_rrule = sqlite3_column_int(stmt, count++);
393 if (0 < instance->has_rrule)
394 instance->has_rrule = 1;
396 instance->latitude = sqlite3_column_double(stmt, count++);
397 instance->longitude = sqlite3_column_double(stmt, count++);
398 instance->has_alarm = sqlite3_column_int(stmt, count++);
399 instance->original_event_id = sqlite3_column_int(stmt, count++);
400 instance->calendar_id = sqlite3_column_int(stmt, count++);
401 instance->last_mod = sqlite3_column_int64(stmt, count++);
403 temp = sqlite3_column_text(stmt, count++);
404 instance->sync_data1 = cal_strdup((const char*)temp);
406 temp = sqlite3_column_text(stmt, count++);
407 instance->organizer_name = cal_strdup((const char*)temp);
409 temp = sqlite3_column_text(stmt, count++);
410 instance->categories = cal_strdup((const char*)temp);
412 instance->has_attendee = sqlite3_column_int(stmt, count++);
414 temp = sqlite3_column_text(stmt, count++);
415 instance->sync_data2 = cal_strdup((const char*)temp);
417 temp = sqlite3_column_text(stmt, count++);
418 instance->sync_data3 = cal_strdup((const char*)temp);
420 temp = sqlite3_column_text(stmt, count++);
421 instance->sync_data4 = cal_strdup((const char*)temp);
426 static void _cal_db_instance_normal_extended_get_property_stmt(sqlite3_stmt *stmt,
427 unsigned int property, int *stmt_count, calendar_record_h record)
429 cal_instance_normal_extended_s* instance = (cal_instance_normal_extended_s*)(record);
430 const unsigned char *temp;
433 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_START:
434 instance->start.type = CALENDAR_TIME_UTIME;
435 *stmt_count = *stmt_count+1;
436 instance->start.time.utime = sqlite3_column_int64(stmt, *stmt_count);
437 *stmt_count = *stmt_count+1;
438 *stmt_count = *stmt_count+1;
440 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_END:
441 instance->end.type = CALENDAR_TIME_UTIME;
442 *stmt_count = *stmt_count+1;
443 instance->end.time.utime = sqlite3_column_int64(stmt, *stmt_count);
444 *stmt_count = *stmt_count+1;
445 *stmt_count = *stmt_count+1;
447 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_SUMMARY:
448 temp = sqlite3_column_text(stmt, *stmt_count);
449 instance->summary = cal_strdup((const char*)temp);
451 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_LOCATION:
452 temp = sqlite3_column_text(stmt, *stmt_count);
453 instance->location = cal_strdup((const char*)temp);
455 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_CALENDAR_ID:
456 instance->calendar_id = sqlite3_column_int(stmt, *stmt_count);
458 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_DESCRIPTION:
459 temp = sqlite3_column_text(stmt, *stmt_count);
460 instance->description = cal_strdup((const char*)temp);
462 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_BUSY_STATUS:
463 instance->busy_status = sqlite3_column_int(stmt, *stmt_count);
465 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_EVENT_STATUS:
466 instance->event_status = sqlite3_column_int(stmt, *stmt_count);
468 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_PRIORITY:
469 instance->priority = sqlite3_column_int(stmt, *stmt_count);
471 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_SENSITIVITY:
472 instance->sensitivity = sqlite3_column_int(stmt, *stmt_count);
474 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_HAS_RRULE:
475 instance->has_rrule = sqlite3_column_int(stmt, *stmt_count);
476 if (0 < instance->has_rrule)
477 instance->has_rrule = 1;
480 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_LATITUDE:
481 instance->latitude = sqlite3_column_double(stmt, *stmt_count);
483 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_LONGITUDE:
484 instance->longitude = sqlite3_column_double(stmt, *stmt_count);
486 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_EVENT_ID:
487 instance->event_id = sqlite3_column_int(stmt, *stmt_count);
489 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_HAS_ALARM:
490 instance->has_alarm = sqlite3_column_int(stmt, *stmt_count);
492 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_ORIGINAL_EVENT_ID:
493 instance->original_event_id = sqlite3_column_int(stmt, *stmt_count);
495 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_LAST_MODIFIED_TIME:
496 instance->last_mod = sqlite3_column_int64(stmt, *stmt_count);
498 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_SYNC_DATA1:
499 temp = sqlite3_column_text(stmt, *stmt_count);
500 instance->sync_data1 = cal_strdup((const char*)temp);
502 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_ORGANIZER_NAME:
503 temp = sqlite3_column_text(stmt, *stmt_count);
504 instance->organizer_name = cal_strdup((const char*)temp);
506 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_CATEGORIES:
507 temp = sqlite3_column_text(stmt, *stmt_count);
508 instance->categories = cal_strdup((const char*)temp);
510 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_HAS_ATTENDEE:
511 instance->has_attendee = sqlite3_column_int(stmt, *stmt_count);
513 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_SYNC_DATA2:
514 temp = sqlite3_column_text(stmt, *stmt_count);
515 instance->sync_data2 = cal_strdup((const char*)temp);
517 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_SYNC_DATA3:
518 temp = sqlite3_column_text(stmt, *stmt_count);
519 instance->sync_data3 = cal_strdup((const char*)temp);
521 case CAL_PROPERTY_INSTANCE_NORMAL_EXTENDED_SYNC_DATA4:
522 temp = sqlite3_column_text(stmt, *stmt_count);
523 instance->sync_data4 = cal_strdup((const char*)temp);
526 sqlite3_column_int(stmt, *stmt_count);
530 *stmt_count = *stmt_count+1;
535 static void _cal_db_instance_normal_extended_get_projection_stmt(sqlite3_stmt *stmt,
536 const unsigned int *projection, const int projection_count,
537 calendar_record_h record)
542 for (i = 0; i < projection_count; i++)
543 _cal_db_instance_normal_extended_get_property_stmt(stmt, projection[i], &stmt_count, record);