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) {
52 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
53 SECURE("query[%s]", query);
58 const unsigned char *temp;
59 calendar_record_h record = NULL;
60 cal_extended_s *extended = NULL;
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);
71 extended = (cal_extended_s *)(record);
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);
81 calendar_list_add((calendar_list_h)list, record);
83 sqlite3_finalize(stmt);
84 return CALENDAR_ERROR_NONE;
87 int cal_db_extended_delete_with_id(int record_id, calendar_record_type_e record_type)
89 char query[CAL_DB_SQL_MAX_LEN] = {0};
92 snprintf(query, sizeof(query), "DELETE FROM %s WHERE record_id=%d AND record_type=%d",
93 CAL_TABLE_EXTENDED, record_id, record_type);
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);
102 return CALENDAR_ERROR_NONE;
105 int cal_db_extended_insert_record(calendar_record_h record, int record_id, calendar_record_type_e record_type, int *id)
109 char query[CAL_DB_SQL_MAX_LEN];
110 cal_extended_s* extended = (cal_extended_s*)(record);
113 RETV_IF(NULL == extended, CALENDAR_ERROR_INVALID_PARAMETER);
114 RETVM_IF(record_id <= 0, CALENDAR_ERROR_INVALID_PARAMETER, "record_id(%d)", record_id);
116 snprintf(query, sizeof(query), "INSERT INTO %s(record_id, "
117 "record_type ,key ,value) "
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);
131 cal_db_util_stmt_bind_text(stmt, 1, extended->key);
134 cal_db_util_stmt_bind_text(stmt, 2, extended->value);
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);
142 index = cal_db_util_last_insert_id();
144 /* cal_record_set_int(record, _calendar_extended.id,index); */
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);
158 return CALENDAR_ERROR_NONE;
161 int cal_db_extended_insert_records(cal_list_s *list_s, int record_id, calendar_record_type_e record_type)
165 calendar_record_h record = NULL;
166 calendar_list_h list = (calendar_list_h)list_s;
168 RETV_IF(NULL == list, CALENDAR_ERROR_INVALID_PARAMETER);
170 calendar_list_get_count(list, &count);
172 return CALENDAR_ERROR_NONE;
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);
180 return CALENDAR_ERROR_NONE;