if db version is the latest, skip checking version logic
[platform/core/pim/calendar-service.git] / server / db / cal_db_plugin_instance_utime.c
1 /*
2  * Calendar Service
3  *
4  * Copyright (c) 2012 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20 #include <stdlib.h>
21
22 #include "cal_internal.h"
23 #include "cal_typedef.h"
24 #include "cal_view.h"
25 #include "cal_record.h"
26
27 #include "cal_db_util.h"
28 #include "cal_db.h"
29 #include "cal_db_query.h"
30 #include "cal_access_control.h"
31 #include "cal_utils.h"
32
33 static int _cal_db_instance_utime_delete_record(int id);
34 static int _cal_db_instance_utime_get_all_records(int offset, int limit, calendar_list_h* out_list);
35 static int _cal_db_instance_utime_get_records_with_query(calendar_query_h query, int offset, int limit, calendar_list_h* out_list);
36 static int _cal_db_instance_utime_get_count(int *out_count);
37 static int _cal_db_instance_utime_get_count_with_query(calendar_query_h query, int *out_count);
38
39 /*
40  * static function
41  */
42 static void _cal_db_instance_utime_get_stmt(sqlite3_stmt *stmt, calendar_record_h record);
43 static void _cal_db_instance_utime_get_property_stmt(sqlite3_stmt *stmt,
44                 unsigned int property, int *stmt_count, calendar_record_h record);
45 static void _cal_db_instance_utime_get_projection_stmt(sqlite3_stmt *stmt,
46                 const unsigned int *projection, const int projection_count,
47                 calendar_record_h record);
48
49 cal_db_plugin_cb_s cal_db_instance_utime_plugin_cb = {
50         .is_query_only = false,
51         .insert_record = NULL,
52         .get_record = NULL,
53         .update_record = NULL,
54         .delete_record = _cal_db_instance_utime_delete_record,
55         .get_all_records = _cal_db_instance_utime_get_all_records,
56         .get_records_with_query = _cal_db_instance_utime_get_records_with_query,
57         .insert_records = NULL,
58         .update_records = NULL,
59         .delete_records = NULL,
60         .get_count = _cal_db_instance_utime_get_count,
61         .get_count_with_query = _cal_db_instance_utime_get_count_with_query,
62         .replace_record = NULL,
63         .replace_records = NULL
64 };
65
66 static int _cal_db_instance_utime_delete_record(int id)
67 {
68         int ret = 0;
69         char query[CAL_DB_SQL_MAX_LEN] = {0};
70
71         RETVM_IF(id < 0, CALENDAR_ERROR_INVALID_PARAMETER,
72                         "Invalid argument: id(%d) < 0", id);
73
74         snprintf(query, sizeof(query),
75                         "DELETE FROM %s "
76                         "WHERE event_id = %d ",
77                         CAL_TABLE_UTIME_INSTANCE,
78                         id);
79
80         ret = cal_db_util_query_exec(query);
81         if (CALENDAR_ERROR_NONE != ret) {
82                 /* LCOV_EXCL_START */
83                 ERR("cal_db_util_query_exec() Fail(%d)", ret);
84                 SECURE("[%s]", query);
85                 return ret;
86                 /* LCOV_EXCL_STOP */
87         }
88
89         return CALENDAR_ERROR_NONE;
90 }
91
92 static int _cal_db_instance_utime_get_all_records(int offset, int limit, calendar_list_h* out_list)
93 {
94         int ret = CALENDAR_ERROR_NONE;
95         char offsetquery[CAL_DB_SQL_MAX_LEN] = {0};
96         char limitquery[CAL_DB_SQL_MAX_LEN] = {0};
97         sqlite3_stmt *stmt = NULL;
98
99         RETV_IF(NULL == out_list, CALENDAR_ERROR_INVALID_PARAMETER);
100
101         ret = calendar_list_create(out_list);
102         RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "calendar_list_create() Fail(%d)", ret);
103
104         if (0 < offset)
105                 snprintf(offsetquery, sizeof(offsetquery), "OFFSET %d", offset);
106         if (0 < limit)
107                 snprintf(limitquery, sizeof(limitquery), "LIMIT %d", limit);
108
109         char *query_str = NULL;
110         cal_db_append_string(&query_str, "SELECT * FROM");
111         cal_db_append_string(&query_str, CAL_VIEW_TABLE_UTIME_INSTANCE);
112         cal_db_append_string(&query_str, limitquery);
113         cal_db_append_string(&query_str, offsetquery);
114
115         ret = cal_db_util_query_prepare(query_str, &stmt);
116         if (CALENDAR_ERROR_NONE != ret) {
117                 /* LCOV_EXCL_START */
118                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
119                 SECURE("query[%s]", query_str);
120                 calendar_list_destroy(*out_list, true);
121                 *out_list = NULL;
122                 free(query_str);
123                 return ret;
124                 /* LCOV_EXCL_STOP */
125         }
126
127         SECURE("[TEST]---------query[%s]", query_str);
128         while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
129                 calendar_record_h record;
130                 ret = calendar_record_create(_calendar_instance_utime_calendar_book._uri, &record);
131                 if (CALENDAR_ERROR_NONE != ret) {
132                         /* LCOV_EXCL_START */
133                         calendar_list_destroy(*out_list, true);
134                         *out_list = NULL;
135                         sqlite3_finalize(stmt);
136                         CAL_FREE(query_str);
137                         return ret;
138                         /* LCOV_EXCL_STOP */
139                 }
140                 _cal_db_instance_utime_get_stmt(stmt, record);
141
142                 ret = calendar_list_add(*out_list, record);
143                 if (CALENDAR_ERROR_NONE != ret) {
144                         /* LCOV_EXCL_START */
145                         calendar_list_destroy(*out_list, true);
146                         *out_list = NULL;
147                         calendar_record_destroy(record, true);
148                         sqlite3_finalize(stmt);
149                         CAL_FREE(query_str);
150                         return ret;
151                         /* LCOV_EXCL_STOP */
152                 }
153         }
154         sqlite3_finalize(stmt);
155         CAL_FREE(query_str);
156
157         return CALENDAR_ERROR_NONE;
158 }
159
160 static int _cal_db_instance_utime_get_records_with_query(calendar_query_h query, int offset, int limit, calendar_list_h* out_list)
161 {
162         cal_query_s *que = NULL;
163         int ret = CALENDAR_ERROR_NONE;
164         char *condition = NULL;
165         char *projection = NULL;
166         GSList *bind_text = NULL, *cursor = NULL;
167         sqlite3_stmt *stmt = NULL;
168         int i = 0;
169         char *table_name;
170
171         que = (cal_query_s *)query;
172
173         if (CAL_STRING_EQUAL == strcmp(que->view_uri, CALENDAR_VIEW_INSTANCE_UTIME_BOOK)) {
174                 table_name = cal_strdup(CAL_VIEW_TABLE_UTIME_INSTANCE);
175         } else if (CAL_STRING_EQUAL == strcmp(que->view_uri, CALENDAR_VIEW_INSTANCE_UTIME_BOOK_EXTENDED)) {
176                 table_name = cal_strdup(CAL_VIEW_TABLE_UTIME_INSTANCE_EXTENDED);
177         } else {
178                 /* LCOV_EXCL_START */
179                 ERR("uri(%s) not support get records with query", que->view_uri);
180                 return CALENDAR_ERROR_INVALID_PARAMETER;
181                 /* LCOV_EXCL_STOP */
182         }
183
184         /* make filter */
185         if (que->filter) {
186                 ret = cal_db_query_create_condition(query, &condition, &bind_text);
187                 if (CALENDAR_ERROR_NONE != ret) {
188                         /* LCOV_EXCL_START */
189                         ERR("cal_db_query_create_condition() Fail(%d), ret");
190                         CAL_FREE(table_name);
191                         return ret;
192                         /* LCOV_EXCL_STOP */
193                 }
194         }
195
196         /* make: projection */
197         ret = cal_db_query_create_projection(query, &projection);
198
199         char *query_str = NULL;
200         /* query: projection */
201         if (projection) {
202                 cal_db_append_string(&query_str, "SELECT");
203                 cal_db_append_string(&query_str, projection);
204                 cal_db_append_string(&query_str, "FROM");
205                 cal_db_append_string(&query_str, table_name);
206                 CAL_FREE(projection);
207         } else {
208                 cal_db_append_string(&query_str, "SELECT * FROM");
209                 cal_db_append_string(&query_str, table_name);
210         }
211         CAL_FREE(table_name);
212
213         /* query: condition */
214         if (condition) {
215                 cal_db_append_string(&query_str, "WHERE (");
216                 cal_db_append_string(&query_str, condition);
217                 cal_db_append_string(&query_str, ")");
218         }
219
220         /* order */
221         char *order = NULL;
222         ret = cal_db_query_create_order(query, condition, &order);
223         if (order) {
224                 cal_db_append_string(&query_str, order);
225                 CAL_FREE(order);
226         }
227         CAL_FREE(condition);
228
229         /* limit, offset */
230         char buf[CAL_STR_SHORT_LEN32] = {0};
231         if (0 < limit) {
232                 snprintf(buf, sizeof(buf), "LIMIT %d", limit);
233                 cal_db_append_string(&query_str, buf);
234
235                 if (0 < offset) {
236                         snprintf(buf, sizeof(buf), "OFFSET %d", offset);
237                         cal_db_append_string(&query_str, buf);
238                 }
239         }
240
241         /* query */
242         ret = cal_db_util_query_prepare(query_str, &stmt);
243         if (CALENDAR_ERROR_NONE != ret) {
244                 /* LCOV_EXCL_START */
245                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
246                 SECURE("query[%s]", query_str);
247                 if (bind_text) {
248                         g_slist_free_full(bind_text, free);
249                         bind_text = NULL;
250                 }
251                 free(query_str);
252                 return ret;
253                 /* LCOV_EXCL_STOP */
254         }
255
256         /* bind text */
257         if (bind_text) {
258                 g_slist_length(bind_text);
259                 for (cursor = bind_text, i = 1; cursor; cursor = cursor->next, i++)
260                         cal_db_util_stmt_bind_text(stmt, i, cursor->data);
261         }
262
263         ret = calendar_list_create(out_list);
264         if (CALENDAR_ERROR_NONE != ret) {
265                 if (bind_text) {
266                         g_slist_free_full(bind_text, free);
267                         bind_text = NULL;
268                 }
269                 /* LCOV_EXCL_START */
270                 ERR("calendar_list_create() Fail");
271                 sqlite3_finalize(stmt);
272                 CAL_FREE(query_str);
273                 return ret;
274                 /* LCOV_EXCL_STOP */
275         }
276
277         while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
278                 calendar_record_h record;
279                 ret = calendar_record_create(que->view_uri, &record);
280                 if (CALENDAR_ERROR_NONE != ret) {
281                         /* LCOV_EXCL_START */
282                         calendar_list_destroy(*out_list, true);
283                         *out_list = NULL;
284
285                         if (bind_text) {
286                                 g_slist_free_full(bind_text, free);
287                                 bind_text = NULL;
288                         }
289                         sqlite3_finalize(stmt);
290                         return ret;
291                         /* LCOV_EXCL_STOP */
292                 }
293                 if (0 < que->projection_count) {
294                         cal_record_set_projection(record,
295                                         que->projection, que->projection_count, que->property_count);
296
297                         _cal_db_instance_utime_get_projection_stmt(stmt,
298                                         que->projection, que->projection_count,
299                                         record);
300                 } else {
301                         _cal_db_instance_utime_get_stmt(stmt, record);
302                 }
303
304                 ret = calendar_list_add(*out_list, record);
305                 if (CALENDAR_ERROR_NONE != ret) {
306                         /* LCOV_EXCL_START */
307                         calendar_list_destroy(*out_list, true);
308                         *out_list = NULL;
309                         calendar_record_destroy(record, true);
310
311                         if (bind_text) {
312                                 g_slist_free_full(bind_text, free);
313                                 bind_text = NULL;
314                         }
315                         sqlite3_finalize(stmt);
316                         CAL_FREE(query_str);
317                         return ret;
318                         /* LCOV_EXCL_STOP */
319                 }
320         }
321
322         if (bind_text) {
323                 g_slist_free_full(bind_text, free);
324                 bind_text = NULL;
325         }
326         sqlite3_finalize(stmt);
327         CAL_FREE(query_str);
328
329         return CALENDAR_ERROR_NONE;
330 }
331
332 static int _cal_db_instance_utime_get_count(int *out_count)
333 {
334         RETV_IF(NULL == out_count, CALENDAR_ERROR_INVALID_PARAMETER);
335
336         char *query_str = NULL;
337         cal_db_append_string(&query_str, "SELECT count(*) FROM");
338         cal_db_append_string(&query_str, CAL_TABLE_UTIME_INSTANCE);
339
340         int ret = 0;
341         int count = 0;
342         ret = cal_db_util_query_get_first_int_result(query_str, NULL, &count);
343         if (CALENDAR_ERROR_NONE != ret) {
344                 /* LCOV_EXCL_START */
345                 ERR("cal_db_util_query_get_first_int_result() Fail");
346                 CAL_FREE(query_str);
347                 return ret;
348         }
349         DBG("count(%d) str[%s]", count, query_str);
350         CAL_FREE(query_str);
351
352         *out_count = count;
353         return CALENDAR_ERROR_NONE;
354 }
355
356 static int _cal_db_instance_utime_get_count_with_query(calendar_query_h query, int *out_count)
357 {
358         cal_query_s *que = NULL;
359         int ret = CALENDAR_ERROR_NONE;
360         char *condition = NULL;
361         char *table_name;
362         int count = 0;
363         GSList *bind_text = NULL;
364
365         que = (cal_query_s *)query;
366
367         if (CAL_STRING_EQUAL == strcmp(que->view_uri, CALENDAR_VIEW_INSTANCE_UTIME_BOOK)) {
368                 table_name = cal_strdup(CAL_VIEW_TABLE_UTIME_INSTANCE);
369         } else if (CAL_STRING_EQUAL == strcmp(que->view_uri, CALENDAR_VIEW_INSTANCE_UTIME_BOOK_EXTENDED)) {
370                 table_name = cal_strdup(CAL_VIEW_TABLE_UTIME_INSTANCE_EXTENDED);
371         } else {
372                 /* LCOV_EXCL_START */
373                 ERR("uri(%s) not support get records with query", que->view_uri);
374                 return CALENDAR_ERROR_INVALID_PARAMETER;
375                 /* LCOV_EXCL_STOP */
376         }
377
378         /* make filter */
379         if (que->filter) {
380                 ret = cal_db_query_create_condition(query, &condition, &bind_text);
381                 if (CALENDAR_ERROR_NONE != ret) {
382                         /* LCOV_EXCL_START */
383                         ERR("cal_db_query_create_condition() Fail(%d), ret");
384                         CAL_FREE(table_name);
385                         return ret;
386                         /* LCOV_EXCL_STOP */
387                 }
388         }
389
390         char *query_str = NULL;
391         /* query: select */
392         cal_db_append_string(&query_str, "SELECT count(*) FROM");
393         cal_db_append_string(&query_str, table_name);
394         CAL_FREE(table_name);
395
396         /* query: condition */
397         if (condition) {
398                 cal_db_append_string(&query_str,  "WHERE (");
399                 cal_db_append_string(&query_str, condition);
400                 cal_db_append_string(&query_str, ")");
401                 CAL_FREE(condition);
402         }
403
404         /* query */
405         ret = cal_db_util_query_get_first_int_result(query_str, bind_text, &count);
406         if (CALENDAR_ERROR_NONE != ret) {
407                 /* LCOV_EXCL_START */
408                 ERR("cal_db_util_query_get_first_int_result() Fail");
409                 if (bind_text) {
410                         g_slist_free_full(bind_text, free);
411                         bind_text = NULL;
412                 }
413
414                 CAL_FREE(query_str);
415                 return ret;
416                 /* LCOV_EXCL_STOP */
417         }
418         DBG("count(%d) str[%s]", count, query_str);
419
420         if (out_count) *out_count = count;
421         if (bind_text) {
422                 g_slist_free_full(bind_text, free);
423                 bind_text = NULL;
424         }
425
426         CAL_FREE(query_str);
427         return CALENDAR_ERROR_NONE;
428 }
429
430 static void _cal_db_instance_utime_get_stmt(sqlite3_stmt *stmt, calendar_record_h record)
431 {
432         cal_instance_utime_s* instance =  (cal_instance_utime_s*)(record);
433         const unsigned char *temp;
434         int count = 0;
435
436         instance->event_id = sqlite3_column_int(stmt, count++);
437         instance->start.type = sqlite3_column_int(stmt, count++);
438         instance->start.time.utime = sqlite3_column_int64(stmt, count++);
439         count++; /* datatime */
440         instance->end.type = sqlite3_column_int(stmt, count++);
441         instance->end.time.utime = sqlite3_column_int64(stmt, count++);
442         count++; /* datatime */
443
444         temp = sqlite3_column_text(stmt, count++);
445         instance->summary = cal_strdup((const char*)temp);
446
447         temp = sqlite3_column_text(stmt, count++);
448         instance->description = cal_strdup((const char*)temp);
449
450         temp = sqlite3_column_text(stmt, count++);
451         instance->location = cal_strdup((const char*)temp);
452
453         instance->busy_status = sqlite3_column_int(stmt, count++);
454
455         instance->event_status = sqlite3_column_int(stmt, count++);
456
457         instance->priority = sqlite3_column_int(stmt, count++);
458
459         instance->sensitivity = sqlite3_column_int(stmt, count++);
460
461         instance->has_rrule = sqlite3_column_int(stmt, count++);
462         if (0 < instance->has_rrule)
463                 instance->has_rrule = 1;
464
465         instance->latitude = sqlite3_column_double(stmt, count++);
466         instance->longitude = sqlite3_column_double(stmt, count++);
467         instance->has_alarm = sqlite3_column_int(stmt, count++);
468         instance->original_event_id = sqlite3_column_int(stmt, count++);
469         instance->calendar_id = sqlite3_column_int(stmt, count++);
470         instance->last_mod = sqlite3_column_int64(stmt, count++);
471
472         temp = sqlite3_column_text(stmt, count++);
473         instance->sync_data1 = cal_strdup((const char*)temp);
474
475         return;
476 }
477
478 static void _cal_db_instance_utime_get_property_stmt(sqlite3_stmt *stmt,
479                 unsigned int property, int *stmt_count, calendar_record_h record)
480 {
481         cal_instance_utime_s* instance =  (cal_instance_utime_s*)(record);
482         const unsigned char *temp;
483
484         switch (property) {
485         case CAL_PROPERTY_INSTANCE_UTIME_START:
486                 instance->start.type = CALENDAR_TIME_UTIME;
487                 *stmt_count = *stmt_count+1;
488                 instance->start.time.utime = sqlite3_column_int64(stmt, *stmt_count);
489                 *stmt_count = *stmt_count+1; /* datatime */
490                 break;
491         case CAL_PROPERTY_INSTANCE_UTIME_END:
492                 instance->end.type = CALENDAR_TIME_UTIME;
493                 sqlite3_column_int(stmt, *stmt_count);
494                 *stmt_count = *stmt_count+1;
495                 instance->end.time.utime = sqlite3_column_int64(stmt, *stmt_count);
496                 *stmt_count = *stmt_count+1; /* datatime */
497                 break;
498         case CAL_PROPERTY_INSTANCE_UTIME_SUMMARY:
499                 temp = sqlite3_column_text(stmt, *stmt_count);
500                 instance->summary = cal_strdup((const char*)temp);
501                 break;
502         case CAL_PROPERTY_INSTANCE_UTIME_LOCATION:
503                 temp = sqlite3_column_text(stmt, *stmt_count);
504                 instance->location = cal_strdup((const char*)temp);
505                 break;
506         case CAL_PROPERTY_INSTANCE_UTIME_BOOK_ID:
507                 instance->calendar_id = sqlite3_column_int(stmt, *stmt_count);
508                 break;
509         case CAL_PROPERTY_INSTANCE_UTIME_DESCRIPTION:
510                 temp = sqlite3_column_text(stmt, *stmt_count);
511                 instance->description = cal_strdup((const char*)temp);
512                 break;
513         case CAL_PROPERTY_INSTANCE_UTIME_BUSY_STATUS:
514                 instance->busy_status = sqlite3_column_int(stmt, *stmt_count);
515                 break;
516         case CAL_PROPERTY_INSTANCE_UTIME_EVENT_STATUS:
517                 instance->event_status = sqlite3_column_int(stmt, *stmt_count);
518                 break;
519         case CAL_PROPERTY_INSTANCE_UTIME_PRIORITY:
520                 instance->priority = sqlite3_column_int(stmt, *stmt_count);
521                 break;
522         case CAL_PROPERTY_INSTANCE_UTIME_SENSITIVITY:
523                 instance->sensitivity = sqlite3_column_int(stmt, *stmt_count);
524                 break;
525         case CAL_PROPERTY_INSTANCE_UTIME_HAS_RRULE:
526                 instance->has_rrule = sqlite3_column_int(stmt, *stmt_count);
527                 if (0 < instance->has_rrule)
528                         instance->has_rrule = 1;
529
530                 break;
531         case CAL_PROPERTY_INSTANCE_UTIME_LATITUDE:
532                 instance->latitude = sqlite3_column_double(stmt, *stmt_count);
533                 break;
534         case CAL_PROPERTY_INSTANCE_UTIME_LONGITUDE:
535                 instance->longitude = sqlite3_column_double(stmt, *stmt_count);
536                 break;
537         case CAL_PROPERTY_INSTANCE_UTIME_EVENT_ID:
538                 instance->event_id = sqlite3_column_int(stmt, *stmt_count);
539                 break;
540         case CAL_PROPERTY_INSTANCE_UTIME_HAS_ALARM:
541                 instance->has_alarm = sqlite3_column_int(stmt, *stmt_count);
542                 break;
543         case CAL_PROPERTY_INSTANCE_UTIME_ORIGINAL_EVENT_ID:
544                 instance->original_event_id = sqlite3_column_int(stmt, *stmt_count);
545                 break;
546         case CAL_PROPERTY_INSTANCE_UTIME_LAST_MODIFIED_TIME:
547                 instance->last_mod = sqlite3_column_int64(stmt, *stmt_count);
548                 break;
549         case CAL_PROPERTY_INSTANCE_UTIME_SYNC_DATA1:
550                 temp = sqlite3_column_text(stmt, *stmt_count);
551                 instance->sync_data1 = cal_strdup((const char*)temp);
552                 break;
553         default:
554                 sqlite3_column_int(stmt, *stmt_count);
555                 break;
556         }
557
558         *stmt_count = *stmt_count+1;
559
560         return;
561 }
562
563 static void _cal_db_instance_utime_get_projection_stmt(sqlite3_stmt *stmt,
564                 const unsigned int *projection, const int projection_count,
565                 calendar_record_h record)
566 {
567         int i = 0;
568         int stmt_count = 0;
569
570         for (i = 0; i < projection_count; i++)
571                 _cal_db_instance_utime_get_property_stmt(stmt, projection[i], &stmt_count, record);
572 }