add comment LCOV_EXCL
[platform/core/pim/calendar-service.git] / server / db / cal_db_plugin_timezone.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_utils.h"
31
32 static int _cal_db_timezone_insert_record(calendar_record_h record, int* id);
33 static int _cal_db_timezone_get_record(int id, calendar_record_h* out_record);
34 static int _cal_db_timezone_update_record(calendar_record_h record);
35 static int _cal_db_timezone_delete_record(int id);
36 static int _cal_db_timezone_get_all_records(int offset, int limit, calendar_list_h* out_list);
37 static int _cal_db_timezone_get_records_with_query(calendar_query_h query, int offset, int limit, calendar_list_h* out_list);
38 static int _cal_db_timezone_delete_records(int ids[], int count);
39 static int _cal_db_timezone_get_count(int *out_count);
40 static int _cal_db_timezone_get_count_with_query(calendar_query_h query, int *out_count);
41 static int _cal_db_timezone_replace_record(calendar_record_h record, int id);
42 static int _cal_db_timezone_replace_records(const calendar_list_h list, int ids[], int count);
43
44 /*
45  * static function
46  */
47 static void _cal_db_timezone_get_stmt(sqlite3_stmt *stmt, calendar_record_h record);
48 static void _cal_db_timezone_get_property_stmt(sqlite3_stmt *stmt,
49                 unsigned int property, int stmt_count, calendar_record_h record);
50 static void _cal_db_timezone_get_projection_stmt(sqlite3_stmt *stmt,
51                 const unsigned int *projection, const int projection_count,
52                 calendar_record_h record);
53 static void _cal_db_timezone_get_stmt(sqlite3_stmt *stmt, calendar_record_h record);
54 static int _cal_db_timezone_update_projection(calendar_record_h record);
55
56 cal_db_plugin_cb_s cal_db_timezone_plugin_cb = {
57         .is_query_only = false,
58         .insert_record = _cal_db_timezone_insert_record,
59         .get_record = _cal_db_timezone_get_record,
60         .update_record = _cal_db_timezone_update_record,
61         .delete_record = _cal_db_timezone_delete_record,
62         .get_all_records = _cal_db_timezone_get_all_records,
63         .get_records_with_query = _cal_db_timezone_get_records_with_query,
64         .insert_records = NULL,
65         .update_records = NULL,
66         .delete_records = _cal_db_timezone_delete_records,
67         .get_count = _cal_db_timezone_get_count,
68         .get_count_with_query = _cal_db_timezone_get_count_with_query,
69         .replace_record = _cal_db_timezone_replace_record,
70         .replace_records = _cal_db_timezone_replace_records
71 };
72
73 static int _cal_db_timezone_insert_record(calendar_record_h record, int* id)
74 {
75         int ret = CALENDAR_ERROR_NONE;
76         int index;
77         int calendar_book_id = 0;
78         char query[CAL_DB_SQL_MAX_LEN];
79         sqlite3_stmt *stmt;
80         cal_timezone_s* timezone =  (cal_timezone_s*)(record);
81         calendar_record_h record_calendar = NULL;
82
83         RETV_IF(NULL == timezone, CALENDAR_ERROR_INVALID_PARAMETER);
84
85         ret = calendar_record_get_int(record,
86                         _calendar_timezone.calendar_book_id, &calendar_book_id);
87         DBG("calendar_book_id(%d)", calendar_book_id);
88
89         ret = cal_db_get_record(_calendar_book._uri, calendar_book_id, &record_calendar);
90         RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "cal_db_get_record() Fail(%d)", ret);
91         calendar_record_destroy(record_calendar, true);
92
93         if (timezone->standard_name) {
94                 snprintf(query, sizeof(query), "SELECT count(*), id FROM %s WHERE standard_name=? ",
95                                 CAL_TABLE_TIMEZONE);
96                 ret = cal_db_util_query_prepare(query, &stmt);
97                 if (CALENDAR_ERROR_NONE != ret) {
98                         /* LCOV_EXCL_START */
99                         ERR("cal_db_util_query_prepare() Fail(%d)", ret);
100                         SECURE("query[%s]", query);
101                         return ret;
102                         /* LCOV_EXCL_STOP */
103                 }
104                 cal_db_util_stmt_bind_text(stmt, 1, timezone->standard_name);
105
106                 ret = cal_db_util_stmt_step(stmt);
107                 if (CAL_SQLITE_ROW != ret) {
108                         /* LCOV_EXCL_START */
109                         ERR("cal_db_util_stmt_step() Fail(%d)", ret);
110                         sqlite3_finalize(stmt);
111                         return ret;
112                         /* LCOV_EXCL_STOP */
113                 }
114
115                 index = 0;
116                 int count = sqlite3_column_int(stmt, index++);
117                 int timezone_id = sqlite3_column_int(stmt, index++);
118                 sqlite3_finalize(stmt);
119
120                 if (0 < count) {
121                         DBG("Already exist which tzid name[%s] id(%d)", timezone->standard_name, timezone_id);
122                         *id = timezone_id;
123                         return CALENDAR_ERROR_NONE;
124                 }
125                 DBG("Not registered timezone in the table, so insert timezone.");
126         }
127
128         snprintf(query, sizeof(query), "INSERT INTO %s(tz_offset_from_gmt ,standard_name, "
129                         "std_start_month ,std_start_position_of_week ,std_start_day, "
130                         "std_start_hour ,standard_bias ,day_light_name ,day_light_start_month, "
131                         "day_light_start_position_of_week ,day_light_start_day, "
132                         "day_light_start_hour ,day_light_bias, calendar_id) "
133                         "VALUES(%d,?,%d,%d,%d,%d,%d,?,%d,%d,%d,%d,%d,%d)",
134                         CAL_TABLE_TIMEZONE,
135                         timezone->tz_offset_from_gmt,
136                         timezone->std_start_month,
137                         timezone->std_start_position_of_week,
138                         timezone->std_start_day,
139                         timezone->std_start_hour,
140                         timezone->standard_bias,
141                         timezone->day_light_start_month,
142                         timezone->day_light_start_position_of_week,
143                         timezone->day_light_start_day,
144                         timezone->day_light_start_hour,
145                         timezone->day_light_bias,
146                         timezone->calendar_id);
147
148         ret = cal_db_util_query_prepare(query, &stmt);
149         if (CALENDAR_ERROR_NONE != ret) {
150                 /* LCOV_EXCL_START */
151                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
152                 SECURE("query[%s]", query);
153                 return ret;
154                 /* LCOV_EXCL_STOP */
155         }
156
157         if (timezone->standard_name)
158                 cal_db_util_stmt_bind_text(stmt, 1, timezone->standard_name);
159
160         if (timezone->day_light_name)
161                 cal_db_util_stmt_bind_text(stmt, 2, timezone->day_light_name);
162
163         ret = cal_db_util_stmt_step(stmt);
164         sqlite3_finalize(stmt);
165         if (CALENDAR_ERROR_NONE != ret) {
166                 /* LCOV_EXCL_START */
167                 ERR("cal_db_util_stmt_step() Fail(%d)", ret);
168                 return ret;
169                 /* LCOV_EXCL_STOP */
170         }
171         index = cal_db_util_last_insert_id();
172
173         if (id)
174                 *id = index;
175
176         return CALENDAR_ERROR_NONE;
177 }
178
179 static int _cal_db_timezone_get_record(int id, calendar_record_h* out_record)
180 {
181         char query[CAL_DB_SQL_MAX_LEN];
182         sqlite3_stmt *stmt = NULL;
183         int ret = 0;
184
185         ret = calendar_record_create(_calendar_timezone._uri, out_record);
186         if (CALENDAR_ERROR_NONE != ret) {
187                 /* LCOV_EXCL_START */
188                 ERR("calendar_record_create() Fail(%d)", ret);
189                 return CALENDAR_ERROR_OUT_OF_MEMORY;
190                 /* LCOV_EXCL_STOP */
191         }
192
193         snprintf(query, sizeof(query), "SELECT * FROM %s WHERE id = %d AND "
194                         "calendar_id IN (select id from %s where deleted = 0)",
195                         CAL_TABLE_TIMEZONE, id,
196                         CAL_TABLE_CALENDAR);
197         ret = cal_db_util_query_prepare(query, &stmt);
198         if (CALENDAR_ERROR_NONE != ret) {
199                 /* LCOV_EXCL_START */
200                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
201                 SECURE("query[%s]", query);
202                 calendar_record_destroy(*out_record, true);
203                 *out_record = NULL;
204                 return ret;
205                 /* LCOV_EXCL_STOP */
206         }
207
208         ret = cal_db_util_stmt_step(stmt);
209         if (CAL_SQLITE_ROW != ret) {
210                 /* LCOV_EXCL_START */
211                 ERR("cal_db_util_stmt_step() Faile%d)", ret);
212                 sqlite3_finalize(stmt);
213                 calendar_record_destroy(*out_record, true);
214                 *out_record = NULL;
215                 if (CALENDAR_ERROR_NONE == ret)
216                         return CALENDAR_ERROR_DB_RECORD_NOT_FOUND;
217                 return ret;
218                 /* LCOV_EXCL_STOP */
219         }
220
221         _cal_db_timezone_get_stmt(stmt, *out_record);
222
223         sqlite3_finalize(stmt);
224         stmt = NULL;
225
226         return CALENDAR_ERROR_NONE;
227 }
228
229 static int _cal_db_timezone_update_record(calendar_record_h record)
230 {
231         char query[CAL_DB_SQL_MAX_LEN] = {0};
232         sqlite3_stmt *stmt = NULL;
233         cal_timezone_s* timezone_info =  (cal_timezone_s*)(record);
234         int ret = 0;
235
236         RETV_IF(NULL == timezone_info, CALENDAR_ERROR_INVALID_PARAMETER);
237
238         if (timezone_info->common.properties_flags)
239                 return _cal_db_timezone_update_projection(record);
240
241         snprintf(query, sizeof(query), "UPDATE %s SET "
242                         "tz_offset_from_gmt=%d,"
243                         "standard_name=?,"
244                         "std_start_month=%d,"
245                         "std_start_position_of_week=%d,"
246                         "std_start_day=%d,"
247                         "std_start_hour=%d,"
248                         "standard_bias=%d,"
249                         "day_light_name=?,"
250                         "day_light_start_month=%d,"
251                         "day_light_start_position_of_week=%d,"
252                         "day_light_start_day=%d,"
253                         "day_light_start_hour=%d,"
254                         "day_light_bias=%d, "
255                         "calendar_id=%d "
256                         "WHERE id = %d",
257                         CAL_TABLE_TIMEZONE,
258                         timezone_info->tz_offset_from_gmt,
259                         timezone_info->std_start_month,
260                         timezone_info->std_start_position_of_week,
261                         timezone_info->std_start_day,
262                         timezone_info->std_start_hour,
263                         timezone_info->standard_bias,
264                         timezone_info->day_light_start_month,
265                         timezone_info->day_light_start_position_of_week,
266                         timezone_info->day_light_start_day,
267                         timezone_info->day_light_start_hour,
268                         timezone_info->day_light_bias,
269                         timezone_info->calendar_id,
270                         timezone_info->index);
271
272         ret = cal_db_util_query_prepare(query, &stmt);
273         if (CALENDAR_ERROR_NONE != ret) {
274                 /* LCOV_EXCL_START */
275                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
276                 SECURE("query[%s]", query);
277                 return ret;
278                 /* LCOV_EXCL_STOP */
279         }
280
281         if (timezone_info->standard_name)
282                 cal_db_util_stmt_bind_text(stmt, 1, timezone_info->standard_name);
283
284         if (timezone_info->day_light_name)
285                 cal_db_util_stmt_bind_text(stmt, 2, timezone_info->day_light_name);
286
287         ret = cal_db_util_stmt_step(stmt);
288         sqlite3_finalize(stmt);
289         if (CALENDAR_ERROR_NONE != ret) {
290                 /* LCOV_EXCL_START */
291                 ERR("cal_db_util_stmt_step() Fail(%d)", ret);
292                 return ret;
293                 /* LCOV_EXCL_STOP */
294         }
295
296         return CALENDAR_ERROR_NONE;
297 }
298
299 static int _cal_db_timezone_delete_record(int id)
300 {
301         char query[CAL_DB_SQL_MAX_LEN] = {0};
302         int ret = 0;
303
304         snprintf(query, sizeof(query), "DELETE FROM %s WHERE id = %d", CAL_TABLE_TIMEZONE, id);
305         ret = cal_db_util_query_exec(query);
306         if (CALENDAR_ERROR_NONE != ret) {
307                 /* LCOV_EXCL_START */
308                 ERR("cal_db_util_query_exec() Fail(%d)", ret);
309                 SECURE("[%s]", query);
310                 return ret;
311                 /* LCOV_EXCL_STOP */
312         }
313
314         return CALENDAR_ERROR_NONE;
315 }
316
317 static int _cal_db_timezone_replace_record(calendar_record_h record, int id)
318 {
319         char query[CAL_DB_SQL_MAX_LEN] = {0};
320         sqlite3_stmt *stmt = NULL;
321         cal_timezone_s* timezone_info =  (cal_timezone_s*)(record);
322         int ret = 0;
323
324         RETV_IF(NULL == timezone_info, CALENDAR_ERROR_INVALID_PARAMETER);
325         timezone_info->index = id;
326
327         if (timezone_info->common.properties_flags)
328                 return _cal_db_timezone_update_projection(record);
329
330         snprintf(query, sizeof(query), "UPDATE %s SET "
331                         "tz_offset_from_gmt=%d,"
332                         "standard_name=?,"
333                         "std_start_month=%d,"
334                         "std_start_position_of_week=%d,"
335                         "std_start_day=%d,"
336                         "std_start_hour=%d,"
337                         "standard_bias=%d,"
338                         "day_light_name=?,"
339                         "day_light_start_month=%d,"
340                         "day_light_start_position_of_week=%d,"
341                         "day_light_start_day=%d,"
342                         "day_light_start_hour=%d,"
343                         "day_light_bias=%d, "
344                         "calendar_id=%d "
345                         "WHERE id = %d",
346                         CAL_TABLE_TIMEZONE,
347                         timezone_info->tz_offset_from_gmt,
348                         timezone_info->std_start_month,
349                         timezone_info->std_start_position_of_week,
350                         timezone_info->std_start_day,
351                         timezone_info->std_start_hour,
352                         timezone_info->standard_bias,
353                         timezone_info->day_light_start_month,
354                         timezone_info->day_light_start_position_of_week,
355                         timezone_info->day_light_start_day,
356                         timezone_info->day_light_start_hour,
357                         timezone_info->day_light_bias,
358                         timezone_info->calendar_id,
359                         id);
360
361         ret = cal_db_util_query_prepare(query, &stmt);
362         if (CALENDAR_ERROR_NONE != ret) {
363                 /* LCOV_EXCL_START */
364                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
365                 SECURE("query[%s]", query);
366                 return ret;
367                 /* LCOV_EXCL_STOP */
368         }
369
370         if (timezone_info->standard_name)
371                 cal_db_util_stmt_bind_text(stmt, 1, timezone_info->standard_name);
372
373         if (timezone_info->day_light_name)
374                 cal_db_util_stmt_bind_text(stmt, 2, timezone_info->day_light_name);
375
376         ret = cal_db_util_stmt_step(stmt);
377         sqlite3_finalize(stmt);
378         if (CALENDAR_ERROR_NONE != ret) {
379                 /* LCOV_EXCL_START */
380                 ERR("cal_db_util_stmt_step() Fail(%d)", ret);
381                 return ret;
382                 /* LCOV_EXCL_STOP */
383         }
384
385         return CALENDAR_ERROR_NONE;
386 }
387
388 static int _cal_db_timezone_get_all_records(int offset, int limit, calendar_list_h* out_list)
389 {
390         int ret = CALENDAR_ERROR_NONE;
391         char query[CAL_DB_SQL_MAX_LEN] = {0};
392         char offsetquery[CAL_DB_SQL_MAX_LEN] = {0};
393         char limitquery[CAL_DB_SQL_MAX_LEN] = {0};
394         sqlite3_stmt *stmt = NULL;
395
396         RETV_IF(NULL == out_list, CALENDAR_ERROR_INVALID_PARAMETER);
397
398         ret = calendar_list_create(out_list);
399         RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "calendar_list_create() Fail(%d)", ret);
400
401         if (0 < offset)
402                 snprintf(offsetquery, sizeof(offsetquery), "OFFSET %d", offset);
403
404         if (0 < limit)
405                 snprintf(limitquery, sizeof(limitquery), "LIMIT %d", limit);
406
407         snprintf(query, sizeof(query), "SELECT * FROM %s where "
408                         "calendar_id IN (select id from %s where deleted = 0) "
409                         "%s %s",
410                         CAL_TABLE_TIMEZONE,
411                         CAL_TABLE_CALENDAR,
412                         limitquery,
413                         offsetquery);
414
415         ret = cal_db_util_query_prepare(query, &stmt);
416         if (CALENDAR_ERROR_NONE != ret) {
417                 /* LCOV_EXCL_START */
418                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
419                 SECURE("query[%s]", query);
420                 calendar_list_destroy(*out_list, true);
421                 *out_list = NULL;
422                 return ret;
423                 /* LCOV_EXCL_STOP */
424         }
425
426         while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
427                 calendar_record_h record;
428                 ret = calendar_record_create(_calendar_timezone._uri, &record);
429                 if (CALENDAR_ERROR_NONE != ret) {
430                         /* LCOV_EXCL_START */
431                         calendar_list_destroy(*out_list, true);
432                         *out_list = NULL;
433                         sqlite3_finalize(stmt);
434                         return ret;
435                         /* LCOV_EXCL_STOP */
436                 }
437                 _cal_db_timezone_get_stmt(stmt, record);
438
439                 ret = calendar_list_add(*out_list, record);
440                 if (CALENDAR_ERROR_NONE != ret) {
441                         /* LCOV_EXCL_START */
442                         calendar_list_destroy(*out_list, true);
443                         *out_list = NULL;
444                         calendar_record_destroy(record, true);
445                         sqlite3_finalize(stmt);
446                         return ret;
447                         /* LCOV_EXCL_STOP */
448                 }
449         }
450
451         sqlite3_finalize(stmt);
452         return CALENDAR_ERROR_NONE;
453 }
454
455 static int _cal_db_timezone_get_records_with_query(calendar_query_h query, int offset, int limit, calendar_list_h* out_list)
456 {
457         cal_query_s *que = NULL;
458         int ret = CALENDAR_ERROR_NONE;
459         char *condition = NULL;
460         char *projection = NULL;
461         char *order = NULL;
462         GSList *bind_text = NULL, *cursor = NULL;
463         char *query_str = NULL;
464         sqlite3_stmt *stmt = NULL;
465         int i = 0;
466
467         que = (cal_query_s *)query;
468
469         /* make filter */
470         if (que->filter) {
471                 ret = cal_db_query_create_condition(query, &condition, &bind_text);
472                 if (CALENDAR_ERROR_NONE != ret) {
473                         /* LCOV_EXCL_START */
474                         ERR("cal_db_query_create_condition() Fail(%d), ret");
475                         return ret;
476                         /* LCOV_EXCL_STOP */
477                 }
478         }
479
480         /* make: projection */
481         ret = cal_db_query_create_projection(query, &projection);
482
483         /* query: projection */
484         if (projection) {
485                 cal_db_append_string(&query_str, "SELECT");
486                 cal_db_append_string(&query_str, projection);
487                 cal_db_append_string(&query_str, "FROM");
488                 cal_db_append_string(&query_str, CAL_TABLE_TIMEZONE);
489                 CAL_FREE(projection);
490         } else {
491                 cal_db_append_string(&query_str, "SELECT * FROM");
492                 cal_db_append_string(&query_str, CAL_TABLE_TIMEZONE);
493         }
494
495         /* query: condition */
496         if (condition) {
497                 cal_db_append_string(&query_str, "WHERE");
498                 cal_db_append_string(&query_str, condition);
499                 CAL_FREE(condition);
500                 cal_db_append_string(&query_str, "AND calendar_id IN (select id from");
501                 cal_db_append_string(&query_str, CAL_TABLE_CALENDAR);
502                 cal_db_append_string(&query_str, "where deleted = 0)");
503         } else {
504                 cal_db_append_string(&query_str, "WHERE calendar_id IN (select id from");
505                 cal_db_append_string(&query_str, CAL_TABLE_CALENDAR);
506                 cal_db_append_string(&query_str, "where deleted = 0)");
507         }
508
509         /* order */
510         ret = cal_db_query_create_order(query, condition, &order);
511         if (order) {
512                 cal_db_append_string(&query_str, order);
513                 CAL_FREE(order);
514         }
515
516         char buf[CAL_STR_SHORT_LEN32] = {0};
517         if (0 < limit) {
518                 snprintf(buf, sizeof(buf), "LIMIT %d", limit);
519                 cal_db_append_string(&query_str, buf);
520                 if (0 < offset) {
521                         snprintf(buf, sizeof(buf), "OFFSET %d", offset);
522                         cal_db_append_string(&query_str, buf);
523                 }
524         }
525
526         /* query */
527         ret = cal_db_util_query_prepare(query_str, &stmt);
528         if (CALENDAR_ERROR_NONE != ret) {
529                 /* LCOV_EXCL_START */
530                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
531                 SECURE("query[%s]", query_str);
532                 if (bind_text) {
533                         g_slist_free_full(bind_text, free);
534                         bind_text = NULL;
535                 }
536                 free(query_str);
537                 return ret;
538                 /* LCOV_EXCL_STOP */
539         }
540
541         /* bind text */
542         if (bind_text) {
543                 for (cursor = bind_text, i = 1; cursor; cursor = cursor->next, i++)
544                         cal_db_util_stmt_bind_text(stmt, i, cursor->data);
545         }
546
547         ret = calendar_list_create(out_list);
548         if (CALENDAR_ERROR_NONE != ret) {
549                 /* LCOV_EXCL_START */
550                 ERR("calendar_list_create() Fail");
551                 if (bind_text) {
552                         g_slist_free_full(bind_text, free);
553                         bind_text = NULL;
554                 }
555                 CAL_FREE(query_str);
556                 sqlite3_finalize(stmt);
557                 return ret;
558                 /* LCOV_EXCL_STOP */
559         }
560
561         while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
562                 calendar_record_h record;
563                 ret = calendar_record_create(_calendar_timezone._uri, &record);
564                 if (CALENDAR_ERROR_NONE != ret) {
565                         /* LCOV_EXCL_START */
566                         calendar_list_destroy(*out_list, true);
567                         *out_list = NULL;
568
569                         if (bind_text) {
570                                 g_slist_free_full(bind_text, free);
571                                 bind_text = NULL;
572                         }
573                         CAL_FREE(query_str);
574                         sqlite3_finalize(stmt);
575                         return ret;
576                         /* LCOV_EXCL_STOP */
577                 }
578                 if (0 < que->projection_count) {
579                         cal_record_set_projection(record,
580                                         que->projection, que->projection_count, que->property_count);
581
582                         _cal_db_timezone_get_projection_stmt(stmt,
583                                         que->projection, que->projection_count,
584                                         record);
585                 } else {
586                         _cal_db_timezone_get_stmt(stmt, record);
587                 }
588
589                 ret = calendar_list_add(*out_list, record);
590                 if (CALENDAR_ERROR_NONE != ret) {
591                         /* LCOV_EXCL_START */
592                         calendar_list_destroy(*out_list, true);
593                         *out_list = NULL;
594                         calendar_record_destroy(record, true);
595
596                         if (bind_text) {
597                                 g_slist_free_full(bind_text, free);
598                                 bind_text = NULL;
599                         }
600                         CAL_FREE(query_str);
601                         sqlite3_finalize(stmt);
602                         return ret;
603                         /* LCOV_EXCL_STOP */
604                 }
605         }
606
607         if (bind_text) {
608                 g_slist_free_full(bind_text, free);
609                 bind_text = NULL;
610         }
611         CAL_FREE(query_str);
612         sqlite3_finalize(stmt);
613
614         return CALENDAR_ERROR_NONE;
615 }
616
617 static int _cal_db_timezone_delete_records(int ids[], int count)
618 {
619         int ret = 0;
620         int i = 0;
621         for (i = 0; i < count; i++) {
622                 ret = _cal_db_timezone_delete_record(ids[i]);
623                 if (CALENDAR_ERROR_NONE != ret) {
624                         /* LCOV_EXCL_START */
625                         ERR("_cal_db_timezone_delete_record() Fail(%d)", ret);
626                         return CALENDAR_ERROR_DB_FAILED;
627                         /* LCOV_EXCL_STOP */
628                 }
629         }
630         return CALENDAR_ERROR_NONE;
631 }
632
633 static int _cal_db_timezone_replace_records(const calendar_list_h list, int ids[], int count)
634 {
635         calendar_record_h record;
636         int i;
637         int ret = 0;
638
639         RETV_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER);
640
641         ret = calendar_list_first(list);
642         if (CALENDAR_ERROR_NONE != ret) {
643                 /* LCOV_EXCL_START */
644                 ERR("list first error");
645                 return ret;
646                 /* LCOV_EXCL_STOP */
647         }
648
649         for (i = 0; i < count; i++) {
650                 if (CALENDAR_ERROR_NONE == calendar_list_get_current_record_p(list, &record)) {
651                         ret = _cal_db_timezone_replace_record(record, ids[i]);
652                         if (CALENDAR_ERROR_NONE != ret) {
653                                 /* LCOV_EXCL_START */
654                                 ERR("_cal_db_timezone_replace_record() Fail(%d)", ret);
655                                 return CALENDAR_ERROR_DB_FAILED;
656                                 /* LCOV_EXCL_STOP */
657                         }
658                 }
659                 if (CALENDAR_ERROR_NO_DATA != calendar_list_next(list))
660                         break;
661         }
662
663         return CALENDAR_ERROR_NONE;
664 }
665
666 static int _cal_db_timezone_get_count(int *out_count)
667 {
668         char query[CAL_DB_SQL_MAX_LEN] = {0};
669         int count = 0;
670         int ret;
671
672         RETV_IF(NULL == out_count, CALENDAR_ERROR_INVALID_PARAMETER);
673
674         snprintf(query, sizeof(query), "SELECT count(*) FROM %s where "
675                         "calendar_id IN (select id from %s where deleted = 0)",
676                         CAL_TABLE_TIMEZONE,
677                         CAL_TABLE_CALENDAR);
678
679         ret = cal_db_util_query_get_first_int_result(query, NULL, &count);
680         if (CALENDAR_ERROR_NONE != ret) {
681                 /* LCOV_EXCL_START */
682                 ERR("cal_db_util_query_get_first_int_result() Fail");
683                 return ret;
684                 /* LCOV_EXCL_STOP */
685         }
686         DBG("%s=%d", query, count);
687
688         *out_count = count;
689         return CALENDAR_ERROR_NONE;
690 }
691
692 static int _cal_db_timezone_get_count_with_query(calendar_query_h query, int *out_count)
693 {
694         cal_query_s *que = NULL;
695         int ret = CALENDAR_ERROR_NONE;
696         char *condition = NULL;
697         char *query_str = NULL;
698         char *table_name;
699         int count = 0;
700         GSList *bind_text = NULL;
701
702         que = (cal_query_s *)query;
703
704         if (CAL_STRING_EQUAL == strcmp(que->view_uri, CALENDAR_VIEW_TIMEZONE)) {
705                 table_name = cal_strdup(CAL_TABLE_TIMEZONE);
706         } else {
707                 /* LCOV_EXCL_START */
708                 ERR("uri(%s) not support get records with query", que->view_uri);
709                 return CALENDAR_ERROR_INVALID_PARAMETER;
710                 /* LCOV_EXCL_STOP */
711         }
712
713         /* make filter */
714         if (que->filter) {
715                 ret = cal_db_query_create_condition(query, &condition, &bind_text);
716                 if (CALENDAR_ERROR_NONE != ret) {
717                         /* LCOV_EXCL_START */
718                         ERR("cal_db_query_create_condition() Fail(%d), ret");
719                         CAL_FREE(table_name);
720                         return ret;
721                         /* LCOV_EXCL_STOP */
722                 }
723         }
724
725         /* query: select */
726         cal_db_append_string(&query_str, "SELECT count(*) FROM");
727         cal_db_append_string(&query_str, table_name);
728         CAL_FREE(table_name);
729
730         /* query: condition */
731         if (condition) {
732                 cal_db_append_string(&query_str, "WHERE");
733                 cal_db_append_string(&query_str, condition);
734                 cal_db_append_string(&query_str, "AND calendar_id IN (select id from");
735                 cal_db_append_string(&query_str, CAL_TABLE_CALENDAR);
736                 cal_db_append_string(&query_str, "where deleted = 0)");
737                 CAL_FREE(condition);
738         } else {
739                 cal_db_append_string(&query_str, "WHERE calendar_id IN (select id from");
740                 cal_db_append_string(&query_str, CAL_TABLE_CALENDAR);
741                 cal_db_append_string(&query_str, "where deleted = 0)");
742         }
743
744         /* query */
745         ret = cal_db_util_query_get_first_int_result(query_str, bind_text, &count);
746         if (CALENDAR_ERROR_NONE != ret) {
747                 /* LCOV_EXCL_START */
748                 ERR("cal_db_util_query_get_first_int_result() Fail");
749                 if (bind_text) {
750                         g_slist_free_full(bind_text, free);
751                         bind_text = NULL;
752                 }
753                 CAL_FREE(query_str);
754                 return ret;
755                 /* LCOV_EXCL_STOP */
756         }
757         DBG("%s=%d", query_str, count);
758
759         *out_count = count;
760
761         if (bind_text) {
762                 g_slist_free_full(bind_text, free);
763                 bind_text = NULL;
764         }
765         CAL_FREE(query_str);
766         return CALENDAR_ERROR_NONE;
767 }
768
769 static void _cal_db_timezone_get_stmt(sqlite3_stmt *stmt, calendar_record_h record)
770 {
771         cal_timezone_s* timezone =  (cal_timezone_s*)(record);
772         int count = 0;
773         const unsigned char *temp;
774
775         timezone->index = sqlite3_column_int(stmt, count++);
776         timezone->tz_offset_from_gmt = sqlite3_column_int(stmt, count++);
777
778         temp = sqlite3_column_text(stmt, count++);
779         timezone->standard_name = cal_strdup((const char *)temp);
780
781         timezone->std_start_month = sqlite3_column_int(stmt, count++);
782         timezone->std_start_position_of_week = sqlite3_column_int(stmt, count++);
783         timezone->std_start_day = sqlite3_column_int(stmt, count++);
784         timezone->std_start_hour = sqlite3_column_int(stmt, count++);
785         timezone->standard_bias = sqlite3_column_int(stmt, count++);
786
787         temp = sqlite3_column_text(stmt, count++);
788         timezone->day_light_name = cal_strdup((const char *)temp);
789
790         timezone->day_light_start_month = sqlite3_column_int(stmt, count++);
791         timezone->day_light_start_position_of_week = sqlite3_column_int(stmt, count++);
792         timezone->day_light_start_day = sqlite3_column_int(stmt, count++);
793         timezone->day_light_start_hour = sqlite3_column_int(stmt, count++);
794         timezone->day_light_bias = sqlite3_column_int(stmt, count++);
795
796         timezone->calendar_id = sqlite3_column_int(stmt, count++);
797 }
798
799 static void _cal_db_timezone_get_property_stmt(sqlite3_stmt *stmt,
800                 unsigned int property, int stmt_count, calendar_record_h record)
801 {
802         cal_timezone_s* timezone =  (cal_timezone_s*)(record);
803         const unsigned char *temp;
804
805         switch (property) {
806         case CAL_PROPERTY_TIMEZONE_ID:
807                 timezone->index = sqlite3_column_int(stmt, stmt_count);
808                 break;
809         case CAL_PROPERTY_TIMEZONE_TZ_OFFSET_FROM_GMT:
810                 timezone->tz_offset_from_gmt = sqlite3_column_int(stmt, stmt_count);
811                 break;
812         case CAL_PROPERTY_TIMEZONE_STANDARD_NAME:
813                 temp = sqlite3_column_text(stmt, stmt_count);
814                 timezone->standard_name = cal_strdup((const char *)temp);
815                 break;
816         case CAL_PROPERTY_TIMEZONE_STD_START_MONTH:
817                 timezone->std_start_month = sqlite3_column_int(stmt, stmt_count);
818                 break;
819         case CAL_PROPERTY_TIMEZONE_STD_START_POSITION_OF_WEEK:
820                 timezone->std_start_position_of_week = sqlite3_column_int(stmt, stmt_count);
821                 break;
822         case CAL_PROPERTY_TIMEZONE_STD_START_DAY:
823                 timezone->std_start_day = sqlite3_column_int(stmt, stmt_count);
824                 break;
825         case CAL_PROPERTY_TIMEZONE_STD_START_HOUR:
826                 timezone->std_start_hour = sqlite3_column_int(stmt, stmt_count);
827                 break;
828         case CAL_PROPERTY_TIMEZONE_STANDARD_BIAS:
829                 timezone->standard_bias = sqlite3_column_int(stmt, stmt_count);
830                 break;
831         case CAL_PROPERTY_TIMEZONE_DAY_LIGHT_NAME:
832                 temp = sqlite3_column_text(stmt, stmt_count);
833                 timezone->day_light_name = cal_strdup((const char *)temp);
834                 break;
835         case CAL_PROPERTY_TIMEZONE_DAY_LIGHT_START_MONTH:
836                 timezone->day_light_start_month = sqlite3_column_int(stmt, stmt_count);
837                 break;
838         case CAL_PROPERTY_TIMEZONE_DAY_LIGHT_START_POSITION_OF_WEEK:
839                 timezone->day_light_start_position_of_week = sqlite3_column_int(stmt, stmt_count);
840                 break;
841         case CAL_PROPERTY_TIMEZONE_DAY_LIGHT_START_DAY:
842                 timezone->day_light_start_day = sqlite3_column_int(stmt, stmt_count);
843                 break;
844         case CAL_PROPERTY_TIMEZONE_DAY_LIGHT_START_HOUR:
845                 timezone->day_light_start_hour = sqlite3_column_int(stmt, stmt_count);
846                 break;
847         case CAL_PROPERTY_TIMEZONE_DAY_LIGHT_BIAS:
848                 timezone->day_light_bias = sqlite3_column_int(stmt, stmt_count);
849                 break;
850         case CAL_PROPERTY_TIMEZONE_CALENDAR_ID:
851                 timezone->calendar_id = sqlite3_column_int(stmt, stmt_count);
852                 break;
853         default:
854                 /* LCOV_EXCL_START */
855                 ERR("Invalid property(0x%x)", property);
856                 break;
857                 /* LCOV_EXCL_STOP */
858         }
859
860         return;
861 }
862
863 static void _cal_db_timezone_get_projection_stmt(sqlite3_stmt *stmt,
864                 const unsigned int *projection, const int projection_count,
865                 calendar_record_h record)
866 {
867         int i = 0;
868
869         for (i = 0; i < projection_count; i++)
870                 _cal_db_timezone_get_property_stmt(stmt, projection[i], i, record);
871 }
872
873 static int _cal_db_timezone_update_projection(calendar_record_h record)
874 {
875         char query[CAL_DB_SQL_MAX_LEN] = {0};
876         sqlite3_stmt *stmt = NULL;
877         cal_timezone_s* timezone =  (cal_timezone_s*)(record);
878         int ret = 0;
879         char* set = NULL;
880         GSList *bind_text = NULL;
881         GSList *cursor = NULL;
882
883         ret = cal_db_query_create_projection_update_set(record, &set, &bind_text);
884         RETV_IF(CALENDAR_ERROR_NONE != ret, ret);
885
886         snprintf(query, sizeof(query), "UPDATE %s SET %s WHERE id = %d",
887                         CAL_TABLE_TIMEZONE, set, timezone->index);
888
889         ret = cal_db_util_query_prepare(query, &stmt);
890         if (CALENDAR_ERROR_NONE != ret) {
891                 /* LCOV_EXCL_START */
892                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
893                 SECURE("query[%s]", query);
894                 free(set);
895                 if (bind_text) {
896                         g_slist_free_full(bind_text, free);
897                         bind_text = NULL;
898                 }
899                 return ret;
900                 /* LCOV_EXCL_STOP */
901         }
902
903         if (bind_text) {
904                 int i = 0;
905                 for (cursor = bind_text, i = 1; cursor; cursor = cursor->next, i++)
906                         cal_db_util_stmt_bind_text(stmt, i, cursor->data);
907         }
908
909         ret = cal_db_util_stmt_step(stmt);
910         sqlite3_finalize(stmt);
911         free(set);
912         if (bind_text) {
913                 g_slist_free_full(bind_text, free);
914                 bind_text = NULL;
915         }
916         if (CALENDAR_ERROR_NONE != ret) {
917                 /* LCOV_EXCL_START */
918                 ERR("cal_db_util_stmt_step() Fail(%d)", ret);
919                 return ret;
920                 /* LCOV_EXCL_STOP */
921         }
922
923         return CALENDAR_ERROR_NONE;
924 }