4 * Copyright (c) 2012 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
22 #include "cal_internal.h"
23 #include "cal_typedef.h"
25 #include "cal_record.h"
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"
34 int cal_db_extended_get_records(int record_id, calendar_record_type_e record_type, cal_list_s *list)
37 char query[CAL_DB_SQL_MAX_LEN] = {0};
38 sqlite3_stmt *stmt = NULL;
40 RETV_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER);
42 snprintf(query, sizeof(query),
44 "WHERE record_id = %d AND "
50 ret = cal_db_util_query_prepare(query, &stmt);
51 if (CALENDAR_ERROR_NONE != ret) {
53 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
54 SECURE("query[%s]", query);
60 const unsigned char *temp;
61 calendar_record_h record = NULL;
62 cal_extended_s *extended = NULL;
64 while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
65 ret = calendar_record_create(_calendar_extended_property._uri, &record);
66 if (CALENDAR_ERROR_NONE != ret) {
68 sqlite3_finalize(stmt);
75 extended = (cal_extended_s *)(record);
77 extended->id = sqlite3_column_int(stmt, count++);
78 extended->record_id = sqlite3_column_int(stmt, count++);
79 extended->record_type = sqlite3_column_int(stmt, count++);
80 temp = sqlite3_column_text(stmt, count++);
81 extended->key = cal_strdup((const char*)temp);
82 temp = sqlite3_column_text(stmt, count++);
83 extended->value = cal_strdup((const char*)temp);
85 calendar_list_add((calendar_list_h)list, record);
87 sqlite3_finalize(stmt);
88 return CALENDAR_ERROR_NONE;
91 int cal_db_extended_delete_with_id(int record_id, calendar_record_type_e record_type)
93 char query[CAL_DB_SQL_MAX_LEN] = {0};
96 snprintf(query, sizeof(query), "DELETE FROM %s WHERE record_id=%d AND record_type=%d",
97 CAL_TABLE_EXTENDED, record_id, record_type);
99 ret = cal_db_util_query_exec(query);
100 if (CALENDAR_ERROR_NONE != ret) {
101 /* LCOV_EXCL_START */
102 ERR("cal_db_util_query_exec() Fail(%d)", ret);
103 SECURE("[%s]", query);
108 return CALENDAR_ERROR_NONE;
111 int cal_db_extended_insert_record(calendar_record_h record, int record_id, calendar_record_type_e record_type, int *id)
115 char query[CAL_DB_SQL_MAX_LEN];
116 cal_extended_s* extended = (cal_extended_s*)(record);
119 RETV_IF(NULL == extended, CALENDAR_ERROR_INVALID_PARAMETER);
120 RETVM_IF(record_id <= 0, CALENDAR_ERROR_INVALID_PARAMETER, "record_id(%d)", record_id);
122 snprintf(query, sizeof(query), "INSERT INTO %s(record_id, "
123 "record_type ,key ,value) "
129 ret = cal_db_util_query_prepare(query, &stmt);
130 if (CALENDAR_ERROR_NONE != ret) {
131 /* LCOV_EXCL_START */
132 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
133 SECURE("query[%s]", query);
139 cal_db_util_stmt_bind_text(stmt, 1, extended->key);
142 cal_db_util_stmt_bind_text(stmt, 2, extended->value);
144 ret = cal_db_util_stmt_step(stmt);
145 sqlite3_finalize(stmt);
146 if (CALENDAR_ERROR_NONE != ret) {
147 /* LCOV_EXCL_START */
148 ERR("cal_db_util_stmt_step() Fail(%d)", ret);
152 index = cal_db_util_last_insert_id();
154 /* cal_record_set_int(record, _calendar_extended.id,index); */
158 if (record_type == CALENDAR_RECORD_TYPE_EVENT || record_type == CALENDAR_RECORD_TYPE_TODO) {
159 snprintf(query, sizeof(query), "UPDATE %s SET has_extended = 1 WHERE id = %d ",
160 CAL_TABLE_SCHEDULE, record_id);
161 ret = cal_db_util_query_exec(query);
162 if (CALENDAR_ERROR_NONE != ret) {
163 /* LCOV_EXCL_START */
164 ERR("cal_db_util_query_exec() Fail(%d)", ret);
165 SECURE("[%s]", query);
170 return CALENDAR_ERROR_NONE;
173 int cal_db_extended_insert_records(cal_list_s *list_s, int record_id, calendar_record_type_e record_type)
177 calendar_record_h record = NULL;
178 calendar_list_h list = (calendar_list_h)list_s;
180 RETV_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER);
182 calendar_list_get_count(list, &count);
184 return CALENDAR_ERROR_NONE;
186 calendar_list_first(list);
187 while (CALENDAR_ERROR_NONE == calendar_list_get_current_record_p(list, &record)) {
188 ret = cal_db_extended_insert_record(record, record_id, record_type, NULL);
189 RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "cal_db_extended_insert_record() Fail(%d)", ret);
190 calendar_list_next(list);
192 return CALENDAR_ERROR_NONE;