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_delete_record(int id);
34 static int _cal_db_instance_normal_get_all_records(int offset, int limit, calendar_list_h* out_list);
35 static int _cal_db_instance_normal_get_records_with_query(calendar_query_h query, int offset, int limit, calendar_list_h* out_list);
36 static int _cal_db_instance_normal_get_count(int *out_count);
37 static int _cal_db_instance_normal_get_count_with_query(calendar_query_h query, int *out_count);
42 static void _cal_db_instance_normal_get_stmt(sqlite3_stmt *stmt, calendar_record_h record);
43 static void _cal_db_instance_normal_get_property_stmt(sqlite3_stmt *stmt,
44 unsigned int property, int *stmt_count, calendar_record_h record);
45 static void _cal_db_instance_normal_get_projection_stmt(sqlite3_stmt *stmt,
46 const unsigned int *projection, const int projection_count,
47 calendar_record_h record);
49 cal_db_plugin_cb_s cal_db_instance_normal_plugin_cb = {
50 .is_query_only = false,
51 .insert_record = NULL,
53 .update_record = NULL,
54 .delete_record = _cal_db_instance_normal_delete_record,
55 .get_all_records = _cal_db_instance_normal_get_all_records,
56 .get_records_with_query = _cal_db_instance_normal_get_records_with_query,
57 .insert_records = NULL,
58 .update_records = NULL,
59 .delete_records = NULL,
60 .get_count = _cal_db_instance_normal_get_count,
61 .get_count_with_query = _cal_db_instance_normal_get_count_with_query,
62 .replace_record = NULL,
63 .replace_records = NULL
66 static int _cal_db_instance_normal_delete_record(int id)
69 char query[CAL_DB_SQL_MAX_LEN] = {0};
71 RETVM_IF(id < 0, CALENDAR_ERROR_INVALID_PARAMETER,
72 "Invalid argument: id(%d) < 0", id);
74 snprintf(query, sizeof(query),
76 "WHERE event_id = %d ",
77 CAL_TABLE_NORMAL_INSTANCE,
80 ret = cal_db_util_query_exec(query);
81 if (CALENDAR_ERROR_NONE != ret) {
82 ERR("cal_db_util_query_exec() Fail(%d)", ret);
83 SECURE("[%s]", query);
87 return CALENDAR_ERROR_NONE;
90 static int _cal_db_instance_normal_get_all_records(int offset, int limit, calendar_list_h* out_list)
92 int ret = CALENDAR_ERROR_NONE;
93 char offsetquery[CAL_DB_SQL_MAX_LEN] = {0};
94 char limitquery[CAL_DB_SQL_MAX_LEN] = {0};
95 sqlite3_stmt *stmt = NULL;
97 RETV_IF(NULL == out_list, CALENDAR_ERROR_INVALID_PARAMETER);
99 ret = calendar_list_create(out_list);
100 RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "calendar_list_create() Fail(%d)", ret);
103 snprintf(offsetquery, sizeof(offsetquery), "OFFSET %d", offset);
105 snprintf(limitquery, sizeof(limitquery), "LIMIT %d", limit);
107 char *query_str = NULL;
108 cal_db_append_string(&query_str, "SELECT * FROM");
109 cal_db_append_string(&query_str, CAL_VIEW_TABLE_NORMAL_INSTANCE);
110 cal_db_append_string(&query_str, limitquery);
111 cal_db_append_string(&query_str, offsetquery);
113 ret = cal_db_util_query_prepare(query_str, &stmt);
114 if (CALENDAR_ERROR_NONE != ret) {
115 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
116 SECURE("query[%s]", query_str);
117 calendar_list_destroy(*out_list, true);
123 SECURE("[TEST]---------query[%s]", query_str);
124 while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
125 calendar_record_h record;
126 ret = calendar_record_create(_calendar_instance_utime_calendar_book._uri, &record);
127 if (CALENDAR_ERROR_NONE != ret) {
128 calendar_list_destroy(*out_list, true);
130 sqlite3_finalize(stmt);
134 _cal_db_instance_normal_get_stmt(stmt, record);
136 ret = calendar_list_add(*out_list, record);
137 if (CALENDAR_ERROR_NONE != ret) {
138 calendar_list_destroy(*out_list, true);
140 calendar_record_destroy(record, true);
141 sqlite3_finalize(stmt);
146 sqlite3_finalize(stmt);
149 return CALENDAR_ERROR_NONE;
152 static int _cal_db_instance_normal_get_records_with_query(calendar_query_h query, int offset, int limit, calendar_list_h* out_list)
154 cal_query_s *que = NULL;
155 int ret = CALENDAR_ERROR_NONE;
156 char *condition = NULL;
157 char *projection = NULL;
158 GSList *bind_text = NULL, *cursor = NULL;
159 sqlite3_stmt *stmt = NULL;
163 que = (cal_query_s *)query;
165 if (CAL_STRING_EQUAL == strcmp(que->view_uri, CALENDAR_VIEW_INSTANCE_UTIME_CALENDAR)) {
166 table_name = cal_strdup(CAL_VIEW_TABLE_NORMAL_INSTANCE);
167 } else if (CAL_STRING_EQUAL == strcmp(que->view_uri, CALENDAR_VIEW_INSTANCE_UTIME_CALENDAR_EXTENDED)) {
168 table_name = cal_strdup(CAL_VIEW_TABLE_NORMAL_INSTANCE_EXTENDED);
170 ERR("uri(%s) not support get records with query", que->view_uri);
171 return CALENDAR_ERROR_INVALID_PARAMETER;
176 ret = cal_db_query_create_condition(query, &condition, &bind_text);
177 if (CALENDAR_ERROR_NONE != ret) {
178 CAL_FREE(table_name);
179 ERR("cal_db_query_create_condition() Fail(%d), ret");
184 /* make: projection */
185 ret = cal_db_query_create_projection(query, &projection);
187 char *query_str = NULL;
188 /* query: projection */
190 cal_db_append_string(&query_str, "SELECT");
191 cal_db_append_string(&query_str, projection);
192 cal_db_append_string(&query_str, "FROM");
193 cal_db_append_string(&query_str, table_name);
194 CAL_FREE(projection);
196 cal_db_append_string(&query_str, "SELECT * FROM");
197 cal_db_append_string(&query_str, table_name);
199 CAL_FREE(table_name);
201 /* query: condition */
203 cal_db_append_string(&query_str, "WHERE (");
204 cal_db_append_string(&query_str, condition);
205 cal_db_append_string(&query_str, ")");
210 ret = cal_db_query_create_order(query, condition, &order);
212 cal_db_append_string(&query_str, order);
218 char buf[CAL_STR_SHORT_LEN32] = {0};
220 snprintf(buf, sizeof(buf), "LIMIT %d", limit);
221 cal_db_append_string(&query_str, buf);
224 snprintf(buf, sizeof(buf), "OFFSET %d", offset);
225 cal_db_append_string(&query_str, buf);
230 ret = cal_db_util_query_prepare(query_str, &stmt);
231 if (CALENDAR_ERROR_NONE != ret) {
232 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
233 SECURE("query[%s]", query_str);
235 g_slist_free_full(bind_text, free);
244 g_slist_length(bind_text);
245 for (cursor = bind_text, i = 1; cursor; cursor = cursor->next, i++)
246 cal_db_util_stmt_bind_text(stmt, i, cursor->data);
249 ret = calendar_list_create(out_list);
250 if (CALENDAR_ERROR_NONE != ret) {
252 g_slist_free_full(bind_text, free);
255 ERR("calendar_list_create() Fail");
256 sqlite3_finalize(stmt);
261 while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
262 calendar_record_h record;
263 ret = calendar_record_create(que->view_uri, &record);
264 if (CALENDAR_ERROR_NONE != ret) {
265 calendar_list_destroy(*out_list, true);
269 g_slist_free_full(bind_text, free);
272 sqlite3_finalize(stmt);
275 if (0 < que->projection_count) {
276 cal_record_set_projection(record,
277 que->projection, que->projection_count, que->property_count);
279 _cal_db_instance_normal_get_projection_stmt(stmt,
280 que->projection, que->projection_count,
283 _cal_db_instance_normal_get_stmt(stmt, record);
286 ret = calendar_list_add(*out_list, record);
287 if (CALENDAR_ERROR_NONE != ret) {
288 calendar_list_destroy(*out_list, true);
290 calendar_record_destroy(record, true);
293 g_slist_free_full(bind_text, free);
296 sqlite3_finalize(stmt);
303 g_slist_free_full(bind_text, free);
306 sqlite3_finalize(stmt);
309 return CALENDAR_ERROR_NONE;
312 static int _cal_db_instance_normal_get_count(int *out_count)
314 RETV_IF(NULL == out_count, CALENDAR_ERROR_INVALID_PARAMETER);
316 char *query_str = NULL;
317 cal_db_append_string(&query_str, "SELECT count(*) FROM");
318 cal_db_append_string(&query_str, CAL_TABLE_NORMAL_INSTANCE);
322 ret = cal_db_util_query_get_first_int_result(query_str, NULL, &count);
323 if (CALENDAR_ERROR_NONE != ret) {
324 ERR("cal_db_util_query_get_first_int_result() Fail");
328 DBG("count(%d) str[%s]", count, query_str);
332 return CALENDAR_ERROR_NONE;
335 static int _cal_db_instance_normal_get_count_with_query(calendar_query_h query, int *out_count)
337 cal_query_s *que = NULL;
338 int ret = CALENDAR_ERROR_NONE;
339 char *condition = NULL;
342 GSList *bind_text = NULL;
344 que = (cal_query_s *)query;
346 if (CAL_STRING_EQUAL == strcmp(que->view_uri, CALENDAR_VIEW_INSTANCE_UTIME_CALENDAR)) {
347 table_name = cal_strdup(CAL_VIEW_TABLE_NORMAL_INSTANCE);
348 } else if (CAL_STRING_EQUAL == strcmp(que->view_uri, CALENDAR_VIEW_INSTANCE_UTIME_CALENDAR_EXTENDED)) {
349 table_name = cal_strdup(CAL_VIEW_TABLE_NORMAL_INSTANCE_EXTENDED);
351 ERR("uri(%s) not support get records with query", que->view_uri);
352 return CALENDAR_ERROR_INVALID_PARAMETER;
357 ret = cal_db_query_create_condition(query, &condition, &bind_text);
358 if (CALENDAR_ERROR_NONE != ret) {
359 CAL_FREE(table_name);
360 ERR("cal_db_query_create_condition() Fail(%d), ret");
365 char *query_str = NULL;
367 cal_db_append_string(&query_str, "SELECT count(*) FROM");
368 cal_db_append_string(&query_str, table_name);
369 CAL_FREE(table_name);
371 /* query: condition */
373 cal_db_append_string(&query_str, "WHERE (");
374 cal_db_append_string(&query_str, condition);
375 cal_db_append_string(&query_str, ")");
380 ret = cal_db_util_query_get_first_int_result(query_str, bind_text, &count);
381 if (CALENDAR_ERROR_NONE != ret) {
382 ERR("cal_db_util_query_get_first_int_result() Fail");
384 g_slist_free_full(bind_text, free);
391 DBG("count(%d) str[%s]", count, query_str);
393 if (out_count) *out_count = count;
395 g_slist_free_full(bind_text, free);
400 return CALENDAR_ERROR_NONE;
403 static void _cal_db_instance_normal_get_stmt(sqlite3_stmt *stmt, calendar_record_h record)
405 cal_instance_normal_s* instance = (cal_instance_normal_s*)(record);
406 const unsigned char *temp;
409 instance->event_id = sqlite3_column_int(stmt, count++);
410 instance->start.type = sqlite3_column_int(stmt, count++);
411 instance->start.time.utime = sqlite3_column_int64(stmt, count++);
412 count++; /* datatime */
413 instance->end.type = sqlite3_column_int(stmt, count++);
414 instance->end.time.utime = sqlite3_column_int64(stmt, count++);
415 count++; /* datatime */
417 temp = sqlite3_column_text(stmt, count++);
418 instance->summary = cal_strdup((const char*)temp);
420 temp = sqlite3_column_text(stmt, count++);
421 instance->description = cal_strdup((const char*)temp);
423 temp = sqlite3_column_text(stmt, count++);
424 instance->location = cal_strdup((const char*)temp);
426 instance->busy_status = sqlite3_column_int(stmt, count++);
428 instance->event_status = sqlite3_column_int(stmt, count++);
430 instance->priority = sqlite3_column_int(stmt, count++);
432 instance->sensitivity = sqlite3_column_int(stmt, count++);
434 instance->has_rrule = sqlite3_column_int(stmt, count++);
435 if (0 < instance->has_rrule)
436 instance->has_rrule = 1;
438 instance->latitude = sqlite3_column_double(stmt, count++);
439 instance->longitude = sqlite3_column_double(stmt, count++);
440 instance->has_alarm = sqlite3_column_int(stmt, count++);
441 instance->original_event_id = sqlite3_column_int(stmt, count++);
442 instance->calendar_id = sqlite3_column_int(stmt, count++);
443 instance->last_mod = sqlite3_column_int64(stmt, count++);
445 temp = sqlite3_column_text(stmt, count++);
446 instance->sync_data1 = cal_strdup((const char*)temp);
451 static void _cal_db_instance_normal_get_property_stmt(sqlite3_stmt *stmt,
452 unsigned int property, int *stmt_count, calendar_record_h record)
454 cal_instance_normal_s* instance = (cal_instance_normal_s*)(record);
455 const unsigned char *temp;
458 case CAL_PROPERTY_INSTANCE_NORMAL_START:
459 instance->start.type = CALENDAR_TIME_UTIME;
460 *stmt_count = *stmt_count+1;
461 instance->start.time.utime = sqlite3_column_int64(stmt, *stmt_count);
462 *stmt_count = *stmt_count+1; /* datatime */
464 case CAL_PROPERTY_INSTANCE_NORMAL_END:
465 instance->end.type = CALENDAR_TIME_UTIME;
466 sqlite3_column_int(stmt, *stmt_count);
467 *stmt_count = *stmt_count+1;
468 instance->end.time.utime = sqlite3_column_int64(stmt, *stmt_count);
469 *stmt_count = *stmt_count+1; /* datatime */
471 case CAL_PROPERTY_INSTANCE_NORMAL_SUMMARY:
472 temp = sqlite3_column_text(stmt, *stmt_count);
473 instance->summary = cal_strdup((const char*)temp);
475 case CAL_PROPERTY_INSTANCE_NORMAL_LOCATION:
476 temp = sqlite3_column_text(stmt, *stmt_count);
477 instance->location = cal_strdup((const char*)temp);
479 case CAL_PROPERTY_INSTANCE_NORMAL_CALENDAR_ID:
480 instance->calendar_id = sqlite3_column_int(stmt, *stmt_count);
482 case CAL_PROPERTY_INSTANCE_NORMAL_DESCRIPTION:
483 temp = sqlite3_column_text(stmt, *stmt_count);
484 instance->description = cal_strdup((const char*)temp);
486 case CAL_PROPERTY_INSTANCE_NORMAL_BUSY_STATUS:
487 instance->busy_status = sqlite3_column_int(stmt, *stmt_count);
489 case CAL_PROPERTY_INSTANCE_NORMAL_EVENT_STATUS:
490 instance->event_status = sqlite3_column_int(stmt, *stmt_count);
492 case CAL_PROPERTY_INSTANCE_NORMAL_PRIORITY:
493 instance->priority = sqlite3_column_int(stmt, *stmt_count);
495 case CAL_PROPERTY_INSTANCE_NORMAL_SENSITIVITY:
496 instance->sensitivity = sqlite3_column_int(stmt, *stmt_count);
498 case CAL_PROPERTY_INSTANCE_NORMAL_HAS_RRULE:
499 instance->has_rrule = sqlite3_column_int(stmt, *stmt_count);
500 if (0 < instance->has_rrule)
501 instance->has_rrule = 1;
504 case CAL_PROPERTY_INSTANCE_NORMAL_LATITUDE:
505 instance->latitude = sqlite3_column_double(stmt, *stmt_count);
507 case CAL_PROPERTY_INSTANCE_NORMAL_LONGITUDE:
508 instance->longitude = sqlite3_column_double(stmt, *stmt_count);
510 case CAL_PROPERTY_INSTANCE_NORMAL_EVENT_ID:
511 instance->event_id = sqlite3_column_int(stmt, *stmt_count);
513 case CAL_PROPERTY_INSTANCE_NORMAL_HAS_ALARM:
514 instance->has_alarm = sqlite3_column_int(stmt, *stmt_count);
516 case CAL_PROPERTY_INSTANCE_NORMAL_ORIGINAL_EVENT_ID:
517 instance->original_event_id = sqlite3_column_int(stmt, *stmt_count);
519 case CAL_PROPERTY_INSTANCE_NORMAL_LAST_MODIFIED_TIME:
520 instance->last_mod = sqlite3_column_int64(stmt, *stmt_count);
522 case CAL_PROPERTY_INSTANCE_NORMAL_SYNC_DATA1:
523 temp = sqlite3_column_text(stmt, *stmt_count);
524 instance->sync_data1 = cal_strdup((const char*)temp);
527 sqlite3_column_int(stmt, *stmt_count);
531 *stmt_count = *stmt_count+1;
536 static void _cal_db_instance_normal_get_projection_stmt(sqlite3_stmt *stmt,
537 const unsigned int *projection, const int projection_count,
538 calendar_record_h record)
543 for (i = 0; i < projection_count; i++)
544 _cal_db_instance_normal_get_property_stmt(stmt, projection[i], &stmt_count, record);