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