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"
27 #include "cal_db_util.h"
28 #include "cal_db_query.h"
29 #include "cal_db_plugin_attendee_helper.h"
30 #include "cal_utils.h"
32 static int _cal_db_attendee_get_all_records(int offset, int limit, calendar_list_h* out_list);
33 static int _cal_db_attendee_get_records_with_query(calendar_query_h query, int offset, int limit, calendar_list_h* out_list);
34 static int _cal_db_attendee_get_count(int *out_count);
35 static int _cal_db_attendee_get_count_with_query(calendar_query_h query, int *out_count);
36 /* static int _cal_db_attendee_get_record(int id, calendar_record_h* out_record) */
38 cal_db_plugin_cb_s cal_db_attendee_plugin_cb = {
39 .is_query_only = false,
40 .insert_record = NULL,
41 .update_record = NULL,
42 .replace_record = NULL,
43 .delete_record = NULL,
44 .insert_records = NULL,
45 .update_records = NULL,
46 .delete_records = NULL,
47 .replace_records = NULL,
49 .get_all_records = _cal_db_attendee_get_all_records,
50 .get_records_with_query = _cal_db_attendee_get_records_with_query,
51 .get_count = _cal_db_attendee_get_count,
52 .get_count_with_query = _cal_db_attendee_get_count_with_query
55 static void _cal_db_attendee_get_stmt(sqlite3_stmt *stmt, calendar_record_h record)
57 cal_attendee_s *attendee = NULL;
59 const unsigned char *temp;
61 attendee = (cal_attendee_s*)(record);
64 attendee->parent_id = sqlite3_column_int(stmt, index++);
66 temp = sqlite3_column_text(stmt, index++);
67 attendee->attendee_name = cal_strdup((const char*)temp);
69 temp = sqlite3_column_text(stmt, index++);
70 attendee->attendee_email = cal_strdup((const char*)temp);
72 temp = sqlite3_column_text(stmt, index++);
73 attendee->attendee_number = cal_strdup((const char*)temp);
75 attendee->attendee_status = sqlite3_column_int(stmt, index++);
76 attendee->attendee_ct_index = sqlite3_column_int(stmt, index++);
77 attendee->attendee_role = sqlite3_column_int(stmt, index++);
78 attendee->attendee_rsvp = sqlite3_column_int(stmt, index++);
80 temp = sqlite3_column_text(stmt, index++);
81 attendee->attendee_group = cal_strdup((const char*)temp);
83 temp = sqlite3_column_text(stmt, index++);
84 attendee->attendee_delegator_uri = cal_strdup((const char*)temp);
86 temp = sqlite3_column_text(stmt, index++);
87 attendee->attendee_uid = cal_strdup((const char*)temp);
89 attendee->attendee_cutype = sqlite3_column_int(stmt, index++);
91 temp = sqlite3_column_text(stmt, index++);
92 attendee->attendee_delegatee_uri = cal_strdup((const char*)temp);
94 temp = sqlite3_column_text(stmt, index++);
95 attendee->attendee_member = cal_strdup((const char*)temp);
97 attendee->id = sqlite3_column_int(stmt, index++);
100 static int _cal_db_attendee_get_all_records(int offset, int limit, calendar_list_h* out_list)
103 char query[CAL_DB_SQL_MAX_LEN] = {0};
104 char offsetquery[CAL_DB_SQL_MAX_LEN] = {0};
105 char limitquery[CAL_DB_SQL_MAX_LEN] = {0};
106 sqlite3_stmt *stmt = NULL;
108 RETV_IF(NULL == out_list, CALENDAR_ERROR_INVALID_PARAMETER);
110 ret = calendar_list_create(out_list);
111 RETVM_IF(CALENDAR_ERROR_NONE != ret, ret, "calendar_list_create() Fail(%d)", ret);
114 snprintf(limitquery, sizeof(limitquery), "LIMIT %d ", limit);
117 snprintf(offsetquery, sizeof(offsetquery), "OFFSET %d ", offset);
119 snprintf(query, sizeof(query), "SELECT *, rowid FROM %s %s %s ",
120 CAL_TABLE_ATTENDEE, limitquery, offsetquery);
122 ret = cal_db_util_query_prepare(query, &stmt);
123 if (CALENDAR_ERROR_NONE != ret) {
124 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
125 SECURE("query[%s]", query);
126 calendar_list_destroy(*out_list, true);
131 calendar_record_h record = NULL;
133 while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
134 ret = calendar_record_create(_calendar_attendee._uri, &record);
135 if (CALENDAR_ERROR_NONE != ret) {
136 calendar_list_destroy(*out_list, true);
138 sqlite3_finalize(stmt);
141 _cal_db_attendee_get_stmt(stmt, record);
143 ret = calendar_list_add(*out_list, record);
144 if (CALENDAR_ERROR_NONE != ret) {
145 calendar_list_destroy(*out_list, true);
147 calendar_record_destroy(record, true);
148 sqlite3_finalize(stmt);
152 sqlite3_finalize(stmt);
154 return CALENDAR_ERROR_NONE;
157 static void _cal_db_attendee_get_property_stmt(sqlite3_stmt *stmt,
158 unsigned int property, int *stmt_count, calendar_record_h record)
160 cal_attendee_s *attendee = NULL;
161 const unsigned char *temp;
163 attendee = (cal_attendee_s*)(record);
166 case CAL_PROPERTY_ATTENDEE_NUMBER:
167 temp = sqlite3_column_text(stmt, *stmt_count);
168 attendee->attendee_number = cal_strdup((const char*)temp);
170 case CAL_PROPERTY_ATTENDEE_CUTYPE:
171 attendee->attendee_cutype = sqlite3_column_int(stmt, *stmt_count);
173 case CAL_PROPERTY_ATTENDEE_CT_INDEX:
174 attendee->attendee_ct_index = sqlite3_column_int(stmt, *stmt_count);
176 case CAL_PROPERTY_ATTENDEE_UID:
177 temp = sqlite3_column_text(stmt, *stmt_count);
178 attendee->attendee_uid = cal_strdup((const char*)temp);
180 case CAL_PROPERTY_ATTENDEE_GROUP:
181 temp = sqlite3_column_text(stmt, *stmt_count);
182 attendee->attendee_group = cal_strdup((const char*)temp);
184 case CAL_PROPERTY_ATTENDEE_EMAIL:
185 temp = sqlite3_column_text(stmt, *stmt_count);
186 attendee->attendee_email = cal_strdup((const char*)temp);
188 case CAL_PROPERTY_ATTENDEE_ROLE:
189 attendee->attendee_role = sqlite3_column_int(stmt, *stmt_count);
191 case CAL_PROPERTY_ATTENDEE_STATUS:
192 attendee->attendee_status = sqlite3_column_int(stmt, *stmt_count);
194 case CAL_PROPERTY_ATTENDEE_RSVP:
195 attendee->attendee_rsvp = sqlite3_column_int(stmt, *stmt_count);
197 case CAL_PROPERTY_ATTENDEE_DELEGATEE_URI:
198 temp = sqlite3_column_text(stmt, *stmt_count);
199 attendee->attendee_delegatee_uri = cal_strdup((const char*)temp);
201 case CAL_PROPERTY_ATTENDEE_DELEGATOR_URI:
202 temp = sqlite3_column_text(stmt, *stmt_count);
203 attendee->attendee_delegator_uri = cal_strdup((const char*)temp);
205 case CAL_PROPERTY_ATTENDEE_NAME:
206 temp = sqlite3_column_text(stmt, *stmt_count);
207 attendee->attendee_name = cal_strdup((const char*)temp);
209 case CAL_PROPERTY_ATTENDEE_MEMBER:
210 temp = sqlite3_column_text(stmt, *stmt_count);
211 attendee->attendee_member = cal_strdup((const char*)temp);
213 case CAL_PROPERTY_ATTENDEE_PARENT_ID:
214 attendee->parent_id = sqlite3_column_int(stmt, *stmt_count);
217 sqlite3_column_int(stmt, *stmt_count);
220 *stmt_count = *stmt_count+1;
223 static void _cal_db_attendee_get_projection_stmt(sqlite3_stmt *stmt,
224 const unsigned int *projection, const int projection_count,
225 calendar_record_h record)
230 for (i = 0; i < projection_count; i++)
231 _cal_db_attendee_get_property_stmt(stmt, projection[i], &stmt_count, record);
234 static int _cal_db_attendee_get_records_with_query(calendar_query_h query, int offset, int limit, calendar_list_h* out_list)
236 cal_query_s *que = NULL;
237 int ret = CALENDAR_ERROR_NONE;
238 char *condition = NULL;
239 char *projection = NULL;
241 GSList *bind_text = NULL, *cursor = NULL;
242 char *query_str = NULL;
243 sqlite3_stmt *stmt = NULL;
247 que = (cal_query_s *)query;
249 if (CAL_STRING_EQUAL == strcmp(que->view_uri, CALENDAR_VIEW_ATTENDEE)) {
250 table_name = cal_strdup(CAL_TABLE_ATTENDEE);
252 ERR("uri(%s) not support get records with query", que->view_uri);
253 return CALENDAR_ERROR_INVALID_PARAMETER;
258 ret = cal_db_query_create_condition(query, &condition, &bind_text);
259 if (CALENDAR_ERROR_NONE != ret) {
260 CAL_FREE(table_name);
261 ERR("cal_db_query_create_condition() Fail(%d), ret");
266 /* make projection */
267 ret = cal_db_query_create_projection(query, &projection);
269 /* query: projection */
271 cal_db_append_string(&query_str, "SELECT");
272 cal_db_append_string(&query_str, projection);
273 cal_db_append_string(&query_str, "FROM");
274 cal_db_append_string(&query_str, table_name);
275 CAL_FREE(projection);
277 cal_db_append_string(&query_str, "SELECT *, rowid FROM ");
278 cal_db_append_string(&query_str, table_name);
280 CAL_FREE(table_name);
282 /* query: condition */
284 cal_db_append_string(&query_str, "WHERE");
285 cal_db_append_string(&query_str, condition);
290 ret = cal_db_query_create_order(query, condition, &order);
292 cal_db_append_string(&query_str, order);
296 char buf[CAL_STR_SHORT_LEN32] = {0};
298 snprintf(buf, sizeof(buf), "LIMIT %d", limit);
299 cal_db_append_string(&query_str, buf);
301 snprintf(buf, sizeof(buf), "OFFSET %d", offset);
302 cal_db_append_string(&query_str, buf);
307 ret = cal_db_util_query_prepare(query_str, &stmt);
308 if (CALENDAR_ERROR_NONE != ret) {
309 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
310 SECURE("query[%s]", query_str);
312 g_slist_free_full(bind_text, free);
321 for (cursor = bind_text, i = 1; cursor; cursor = cursor->next, i++)
322 cal_db_util_stmt_bind_text(stmt, i, cursor->data);
325 ret = calendar_list_create(out_list);
326 if (CALENDAR_ERROR_NONE != ret) {
328 g_slist_free_full(bind_text, free);
332 ERR("calendar_list_create() Fail");
333 sqlite3_finalize(stmt);
337 while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
338 calendar_record_h record;
339 ret = calendar_record_create(_calendar_attendee._uri, &record);
340 if (CALENDAR_ERROR_NONE != ret) {
341 calendar_list_destroy(*out_list, true);
345 g_slist_free_full(bind_text, free);
349 sqlite3_finalize(stmt);
352 if (0 < que->projection_count) {
353 cal_record_set_projection(record,
354 que->projection, que->projection_count, que->property_count);
356 _cal_db_attendee_get_projection_stmt(stmt,
357 que->projection, que->projection_count,
360 _cal_db_attendee_get_stmt(stmt, record);
363 ret = calendar_list_add(*out_list, record);
364 if (CALENDAR_ERROR_NONE != ret) {
365 calendar_list_destroy(*out_list, true);
367 calendar_record_destroy(record, true);
370 g_slist_free_full(bind_text, free);
374 sqlite3_finalize(stmt);
380 g_slist_free_full(bind_text, free);
384 sqlite3_finalize(stmt);
386 return CALENDAR_ERROR_NONE;
389 static int _cal_db_attendee_get_count(int *out_count)
392 char query[CAL_DB_SQL_MAX_LEN] = {0};
395 RETV_IF(NULL == out_count, CALENDAR_ERROR_INVALID_PARAMETER);
397 snprintf(query, sizeof(query), "SELECT count(*) FROM %s ", CAL_TABLE_ATTENDEE);
399 ret = cal_db_util_query_get_first_int_result(query, NULL, &count);
400 if (CALENDAR_ERROR_NONE != ret) {
401 ERR("cal_db_util_query_get_first_int_result() failed");
404 DBG("%s=%d", query, count);
407 return CALENDAR_ERROR_NONE;
410 static int _cal_db_attendee_get_count_with_query(calendar_query_h query, int *out_count)
412 cal_query_s *que = NULL;
413 int ret = CALENDAR_ERROR_NONE;
414 char *condition = NULL;
415 char *query_str = NULL;
418 GSList *bind_text = NULL;
420 que = (cal_query_s *)query;
422 if (CAL_STRING_EQUAL == strcmp(que->view_uri, CALENDAR_VIEW_ATTENDEE)) {
423 table_name = cal_strdup(CAL_TABLE_ATTENDEE);
425 ERR("uri(%s) not support get records with query", que->view_uri);
426 return CALENDAR_ERROR_INVALID_PARAMETER;
431 ret = cal_db_query_create_condition(query, &condition, &bind_text);
432 if (CALENDAR_ERROR_NONE != ret) {
433 CAL_FREE(table_name);
434 ERR("cal_db_query_create_condition() Fail(%d), ret");
440 cal_db_append_string(&query_str, "SELECT count(*) FROM");
441 cal_db_append_string(&query_str, table_name);
442 CAL_FREE(table_name);
444 CAL_FREE(table_name);
446 /* query: condition */
448 cal_db_append_string(&query_str, "WHERE");
449 cal_db_append_string(&query_str, condition);
454 ret = cal_db_util_query_get_first_int_result(query_str, bind_text, &count);
455 if (CALENDAR_ERROR_NONE != ret) {
456 ERR("cal_db_util_query_get_first_int_result() failed");
458 g_slist_free_full(bind_text, free);
464 DBG("%s=%d", query_str, count);
469 g_slist_free_full(bind_text, free);
473 return CALENDAR_ERROR_NONE;