cbaec3656e4640345168318d4fc282d59740f7e4
[platform/core/pim/calendar-service.git] / server / db / cal_db_plugin_extended_helper.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 #include "cal_list.h"
27
28 #include "cal_db.h"
29 #include "cal_db_util.h"
30 #include "cal_db_query.h"
31 #include "cal_db_plugin_extended_helper.h"
32 #include "cal_utils.h"
33
34 int cal_db_extended_get_records(int record_id, calendar_record_type_e record_type, cal_list_s *list)
35 {
36         int ret;
37         char query[CAL_DB_SQL_MAX_LEN] = {0};
38         sqlite3_stmt *stmt = NULL;
39
40         RETV_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER);
41
42         snprintf(query, sizeof(query),
43                         "SELECT * FROM %s "
44                         "WHERE record_id = %d AND "
45                         "record_type = %d",
46                         CAL_TABLE_EXTENDED,
47                         record_id,
48                         record_type);
49
50         ret = cal_db_util_query_prepare(query, &stmt);
51         if (CALENDAR_ERROR_NONE != ret) {
52                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
53                 SECURE("query[%s]", query);
54                 return ret;
55         }
56
57         int count = 0;
58         const unsigned char *temp;
59         calendar_record_h record = NULL;
60         cal_extended_s *extended = NULL;
61
62         while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
63                 ret = calendar_record_create(_calendar_extended_property._uri, &record);
64                 if (CALENDAR_ERROR_NONE != ret) {
65                         sqlite3_finalize(stmt);
66                         cal_list_clear(list);
67                         return ret;
68                 }
69
70                 count = 0;
71                 extended = (cal_extended_s *)(record);
72
73                 extended->id = sqlite3_column_int(stmt, count++);
74                 extended->record_id = sqlite3_column_int(stmt, count++);
75                 extended->record_type = sqlite3_column_int(stmt, count++);
76                 temp = sqlite3_column_text(stmt, count++);
77                 extended->key = cal_strdup((const char*)temp);
78                 temp = sqlite3_column_text(stmt, count++);
79                 extended->value = cal_strdup((const char*)temp);
80
81                 calendar_list_add((calendar_list_h)list, record);
82         }
83         sqlite3_finalize(stmt);
84         return CALENDAR_ERROR_NONE;
85 }
86
87 int cal_db_extended_delete_with_id(int record_id, calendar_record_type_e record_type)
88 {
89         char query[CAL_DB_SQL_MAX_LEN] = {0};
90         int ret = 0;
91
92         snprintf(query, sizeof(query), "DELETE FROM %s WHERE record_id=%d AND record_type=%d",
93                         CAL_TABLE_EXTENDED, record_id, record_type);
94
95         ret = cal_db_util_query_exec(query);
96         if (CALENDAR_ERROR_NONE != ret) {
97                 ERR("cal_db_util_query_exec() Fail(%d)", ret);
98                 SECURE("[%s]", query);
99                 return ret;
100         }
101
102         return CALENDAR_ERROR_NONE;
103 }
104
105 int cal_db_extended_insert_record(calendar_record_h record, int record_id, calendar_record_type_e record_type, int *id)
106 {
107         int index;
108         sqlite3_stmt *stmt;
109         char query[CAL_DB_SQL_MAX_LEN];
110         cal_extended_s* extended =  (cal_extended_s*)(record);
111         int ret;
112
113         RETV_IF(NULL == extended, CALENDAR_ERROR_INVALID_PARAMETER);
114         RETVM_IF(record_id <= 0, CALENDAR_ERROR_INVALID_PARAMETER, "record_id(%d)", record_id);
115
116         snprintf(query, sizeof(query), "INSERT INTO %s(record_id, "
117                         "record_type ,key ,value) "
118                         "VALUES(%d,%d,?,?)",
119                         CAL_TABLE_EXTENDED,
120                         record_id,
121                         record_type);
122
123         ret = cal_db_util_query_prepare(query, &stmt);
124         if (CALENDAR_ERROR_NONE != ret) {
125                 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
126                 SECURE("query[%s]", query);
127                 return ret;
128         }
129
130         if (extended->key)
131                 cal_db_util_stmt_bind_text(stmt, 1, extended->key);
132
133         if (extended->value)
134                 cal_db_util_stmt_bind_text(stmt, 2, extended->value);
135
136         ret = cal_db_util_stmt_step(stmt);
137         sqlite3_finalize(stmt);
138         if (CALENDAR_ERROR_NONE != ret) {
139                 ERR("cal_db_util_stmt_step() Fail(%d)", ret);
140                 return ret;
141         }
142         index = cal_db_util_last_insert_id();
143
144         /* cal_record_set_int(record, _calendar_extended.id,index); */
145         if (id)
146                 *id = index;
147
148         if (record_type == CALENDAR_RECORD_TYPE_EVENT || record_type == CALENDAR_RECORD_TYPE_TODO) {
149                 snprintf(query, sizeof(query), "UPDATE %s SET has_extended = 1 WHERE id = %d ",
150                                 CAL_TABLE_SCHEDULE, record_id);
151                 ret = cal_db_util_query_exec(query);
152                 if (CALENDAR_ERROR_NONE != ret) {
153                         ERR("cal_db_util_query_exec() Fail(%d)", ret);
154                         SECURE("[%s]", query);
155                         return ret;
156                 }
157         }
158         return CALENDAR_ERROR_NONE;
159 }
160
161 int cal_db_extended_insert_records(cal_list_s *list_s, int record_id, calendar_record_type_e record_type)
162 {
163         int ret;
164         int count = 0;
165         calendar_record_h record = NULL;
166         calendar_list_h list = (calendar_list_h)list_s;
167
168         RETV_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER);
169
170         calendar_list_get_count(list, &count);
171         if (0 == count)
172                 return CALENDAR_ERROR_NONE;
173
174         calendar_list_first(list);
175         while (CALENDAR_ERROR_NONE == calendar_list_get_current_record_p(list, &record)) {
176                 ret = cal_db_extended_insert_record(record, record_id, record_type, NULL);
177                 RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "cal_db_extended_insert_record() Fail(%d)", ret);
178                 calendar_list_next(list);
179         }
180         return CALENDAR_ERROR_NONE;
181 }
182