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 /* LCOV_EXCL_START */
125 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
126 SECURE("query[%s]", query);
127 calendar_list_destroy(*out_list, true);
133 calendar_record_h record = NULL;
135 while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
136 ret = calendar_record_create(_calendar_attendee._uri, &record);
137 if (CALENDAR_ERROR_NONE != ret) {
138 /* LCOV_EXCL_START */
139 ERR("calendar_record_create() Fail(%d)", ret);
140 calendar_list_destroy(*out_list, true);
142 sqlite3_finalize(stmt);
146 _cal_db_attendee_get_stmt(stmt, record);
148 ret = calendar_list_add(*out_list, record);
149 if (CALENDAR_ERROR_NONE != ret) {
150 /* LCOV_EXCL_START */
151 ERR("calendar_list_add() Fail(%d)", ret);
152 calendar_list_destroy(*out_list, true);
154 calendar_record_destroy(record, true);
155 sqlite3_finalize(stmt);
160 sqlite3_finalize(stmt);
162 return CALENDAR_ERROR_NONE;
165 static void _cal_db_attendee_get_property_stmt(sqlite3_stmt *stmt,
166 unsigned int property, int *stmt_count, calendar_record_h record)
168 cal_attendee_s *attendee = NULL;
169 const unsigned char *temp;
171 attendee = (cal_attendee_s*)(record);
174 case CAL_PROPERTY_ATTENDEE_NUMBER:
175 temp = sqlite3_column_text(stmt, *stmt_count);
176 attendee->attendee_number = cal_strdup((const char*)temp);
178 case CAL_PROPERTY_ATTENDEE_CUTYPE:
179 attendee->attendee_cutype = sqlite3_column_int(stmt, *stmt_count);
181 case CAL_PROPERTY_ATTENDEE_CT_INDEX:
182 attendee->attendee_ct_index = sqlite3_column_int(stmt, *stmt_count);
184 case CAL_PROPERTY_ATTENDEE_UID:
185 temp = sqlite3_column_text(stmt, *stmt_count);
186 attendee->attendee_uid = cal_strdup((const char*)temp);
188 case CAL_PROPERTY_ATTENDEE_GROUP:
189 temp = sqlite3_column_text(stmt, *stmt_count);
190 attendee->attendee_group = cal_strdup((const char*)temp);
192 case CAL_PROPERTY_ATTENDEE_EMAIL:
193 temp = sqlite3_column_text(stmt, *stmt_count);
194 attendee->attendee_email = cal_strdup((const char*)temp);
196 case CAL_PROPERTY_ATTENDEE_ROLE:
197 attendee->attendee_role = sqlite3_column_int(stmt, *stmt_count);
199 case CAL_PROPERTY_ATTENDEE_STATUS:
200 attendee->attendee_status = sqlite3_column_int(stmt, *stmt_count);
202 case CAL_PROPERTY_ATTENDEE_RSVP:
203 attendee->attendee_rsvp = sqlite3_column_int(stmt, *stmt_count);
205 case CAL_PROPERTY_ATTENDEE_DELEGATEE_URI:
206 temp = sqlite3_column_text(stmt, *stmt_count);
207 attendee->attendee_delegatee_uri = cal_strdup((const char*)temp);
209 case CAL_PROPERTY_ATTENDEE_DELEGATOR_URI:
210 temp = sqlite3_column_text(stmt, *stmt_count);
211 attendee->attendee_delegator_uri = cal_strdup((const char*)temp);
213 case CAL_PROPERTY_ATTENDEE_NAME:
214 temp = sqlite3_column_text(stmt, *stmt_count);
215 attendee->attendee_name = cal_strdup((const char*)temp);
217 case CAL_PROPERTY_ATTENDEE_MEMBER:
218 temp = sqlite3_column_text(stmt, *stmt_count);
219 attendee->attendee_member = cal_strdup((const char*)temp);
221 case CAL_PROPERTY_ATTENDEE_PARENT_ID:
222 attendee->parent_id = sqlite3_column_int(stmt, *stmt_count);
225 sqlite3_column_int(stmt, *stmt_count);
228 *stmt_count = *stmt_count+1;
231 static void _cal_db_attendee_get_projection_stmt(sqlite3_stmt *stmt,
232 const unsigned int *projection, const int projection_count,
233 calendar_record_h record)
238 for (i = 0; i < projection_count; i++)
239 _cal_db_attendee_get_property_stmt(stmt, projection[i], &stmt_count, record);
242 static int _cal_db_attendee_get_records_with_query(calendar_query_h query, int offset, int limit, calendar_list_h* out_list)
244 cal_query_s *que = NULL;
245 int ret = CALENDAR_ERROR_NONE;
246 char *condition = NULL;
247 char *projection = NULL;
249 GSList *bind_text = NULL, *cursor = NULL;
250 char *query_str = NULL;
251 sqlite3_stmt *stmt = NULL;
255 que = (cal_query_s *)query;
257 if (CAL_STRING_EQUAL == strcmp(que->view_uri, CALENDAR_VIEW_ATTENDEE)) {
258 table_name = cal_strdup(CAL_TABLE_ATTENDEE);
260 /* LCOV_EXCL_START */
261 ERR("uri(%s) not support get records with query", que->view_uri);
262 return CALENDAR_ERROR_INVALID_PARAMETER;
268 ret = cal_db_query_create_condition(query, &condition, &bind_text);
269 if (CALENDAR_ERROR_NONE != ret) {
270 /* LCOV_EXCL_START */
271 ERR("cal_db_query_create_condition() Fail(%d), ret");
272 CAL_FREE(table_name);
278 /* make projection */
279 ret = cal_db_query_create_projection(query, &projection);
281 /* query: projection */
283 cal_db_append_string(&query_str, "SELECT");
284 cal_db_append_string(&query_str, projection);
285 cal_db_append_string(&query_str, "FROM");
286 cal_db_append_string(&query_str, table_name);
287 CAL_FREE(projection);
289 cal_db_append_string(&query_str, "SELECT *, rowid FROM ");
290 cal_db_append_string(&query_str, table_name);
292 CAL_FREE(table_name);
294 /* query: condition */
296 cal_db_append_string(&query_str, "WHERE");
297 cal_db_append_string(&query_str, condition);
302 ret = cal_db_query_create_order(query, condition, &order);
304 cal_db_append_string(&query_str, order);
308 char buf[CAL_STR_SHORT_LEN32] = {0};
310 snprintf(buf, sizeof(buf), "LIMIT %d", limit);
311 cal_db_append_string(&query_str, buf);
313 snprintf(buf, sizeof(buf), "OFFSET %d", offset);
314 cal_db_append_string(&query_str, buf);
319 ret = cal_db_util_query_prepare(query_str, &stmt);
320 if (CALENDAR_ERROR_NONE != ret) {
321 /* LCOV_EXCL_START */
322 ERR("cal_db_util_query_prepare() Fail(%d)", ret);
323 SECURE("query[%s]", query_str);
325 g_slist_free_full(bind_text, free);
335 for (cursor = bind_text, i = 1; cursor; cursor = cursor->next, i++)
336 cal_db_util_stmt_bind_text(stmt, i, cursor->data);
339 ret = calendar_list_create(out_list);
340 if (CALENDAR_ERROR_NONE != ret) {
341 /* LCOV_EXCL_START */
342 ERR("calendar_list_create() Fail");
344 g_slist_free_full(bind_text, free);
348 sqlite3_finalize(stmt);
353 while (CAL_SQLITE_ROW == cal_db_util_stmt_step(stmt)) {
354 calendar_record_h record;
355 ret = calendar_record_create(_calendar_attendee._uri, &record);
356 if (CALENDAR_ERROR_NONE != ret) {
357 calendar_list_destroy(*out_list, true);
361 g_slist_free_full(bind_text, free);
365 sqlite3_finalize(stmt);
368 if (0 < que->projection_count) {
369 cal_record_set_projection(record,
370 que->projection, que->projection_count, que->property_count);
372 _cal_db_attendee_get_projection_stmt(stmt,
373 que->projection, que->projection_count,
376 _cal_db_attendee_get_stmt(stmt, record);
379 ret = calendar_list_add(*out_list, record);
380 if (CALENDAR_ERROR_NONE != ret) {
381 calendar_list_destroy(*out_list, true);
383 calendar_record_destroy(record, true);
386 g_slist_free_full(bind_text, free);
390 sqlite3_finalize(stmt);
396 g_slist_free_full(bind_text, free);
400 sqlite3_finalize(stmt);
402 return CALENDAR_ERROR_NONE;
405 static int _cal_db_attendee_get_count(int *out_count)
408 char query[CAL_DB_SQL_MAX_LEN] = {0};
411 RETV_IF(NULL == out_count, CALENDAR_ERROR_INVALID_PARAMETER);
413 snprintf(query, sizeof(query), "SELECT count(*) FROM %s ", CAL_TABLE_ATTENDEE);
415 ret = cal_db_util_query_get_first_int_result(query, NULL, &count);
416 if (CALENDAR_ERROR_NONE != ret) {
417 /* LCOV_EXCL_START */
418 ERR("cal_db_util_query_get_first_int_result() failed");
422 DBG("%s=%d", query, count);
425 return CALENDAR_ERROR_NONE;
428 static int _cal_db_attendee_get_count_with_query(calendar_query_h query, int *out_count)
430 cal_query_s *que = NULL;
431 int ret = CALENDAR_ERROR_NONE;
432 char *condition = NULL;
433 char *query_str = NULL;
436 GSList *bind_text = NULL;
438 que = (cal_query_s *)query;
440 if (CAL_STRING_EQUAL == strcmp(que->view_uri, CALENDAR_VIEW_ATTENDEE)) {
441 table_name = cal_strdup(CAL_TABLE_ATTENDEE);
443 /* LCOV_EXCL_START */
444 ERR("uri(%s) not support get records with query", que->view_uri);
445 return CALENDAR_ERROR_INVALID_PARAMETER;
451 ret = cal_db_query_create_condition(query, &condition, &bind_text);
452 if (CALENDAR_ERROR_NONE != ret) {
453 /* LCOV_EXCL_START */
454 ERR("cal_db_query_create_condition() Fail(%d), ret");
455 CAL_FREE(table_name);
462 cal_db_append_string(&query_str, "SELECT count(*) FROM");
463 cal_db_append_string(&query_str, table_name);
464 CAL_FREE(table_name);
466 CAL_FREE(table_name);
468 /* query: condition */
470 cal_db_append_string(&query_str, "WHERE");
471 cal_db_append_string(&query_str, condition);
476 ret = cal_db_util_query_get_first_int_result(query_str, bind_text, &count);
477 if (CALENDAR_ERROR_NONE != ret) {
478 /* LCOV_EXCL_START */
479 ERR("cal_db_util_query_get_first_int_result() failed");
481 g_slist_free_full(bind_text, free);
488 DBG("%s=%d", query_str, count);
493 g_slist_free_full(bind_text, free);
497 return CALENDAR_ERROR_NONE;