4 * Copyright (c) 2010 - 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.
20 #include "ctsvc_internal.h"
21 #include "ctsvc_db_schema.h"
22 #include "ctsvc_db_sqlite.h"
23 #include "ctsvc_db_utils.h"
24 #include "ctsvc_db_init.h"
25 #include "ctsvc_db_access_control.h"
26 #include "ctsvc_db_plugin_activity_photo_helper.h"
27 #include "ctsvc_record.h"
28 #include "ctsvc_notification.h"
29 #include "ctsvc_db_query.h"
30 #include "ctsvc_list.h"
33 static int __ctsvc_db_activity_photo_insert_record(contacts_record_h record, int *id)
37 char query[CTS_SQL_MAX_LEN] = {0};
38 ctsvc_activity_photo_s *activity_photo = (ctsvc_activity_photo_s*)record;
40 RETV_IF(NULL == activity_photo->photo_url, CONTACTS_ERROR_INVALID_PARAMETER);
42 ret = ctsvc_begin_trans();
43 if (CONTACTS_ERROR_NONE != ret) {
45 ERR("ctsvc_begin_trans() Fail(%d)", ret);
50 snprintf(query, sizeof(query),
51 "SELECT addressbook_id from "CTSVC_DB_VIEW_CONTACT" "
52 "WHERE contact_id = (SELECT contact_id from "CTS_TABLE_ACTIVITIES" WHERE id = %d)", activity_photo->activity_id);
53 ret = ctsvc_query_get_first_int_result(query, &addressbook_id);
54 if (CONTACTS_ERROR_NONE != ret) {
56 ctsvc_end_trans(false);
57 if (CONTACTS_ERROR_NO_DATA == ret) {
58 ERR("No data : activity_id (%d) is not exist", activity_photo->activity_id);
59 return CONTACTS_ERROR_INVALID_PARAMETER;
61 ERR("ctsvc_query_get_first_int_result() Fail(%d)", ret);
67 if (false == ctsvc_have_ab_write_permission(addressbook_id, false)) {
69 ERR("No permission in this addresbook_id(%d)", addressbook_id);
70 ctsvc_end_trans(false);
71 return CONTACTS_ERROR_PERMISSION_DENIED;
75 ret = ctsvc_db_activity_photo_insert(record, activity_photo->activity_id, id);
76 if (CONTACTS_ERROR_NONE != ret) {
78 ERR("ctsvc_db_activity_photo_insert() Fail(%d)", ret);
79 ctsvc_end_trans(false);
84 ret = ctsvc_end_trans(true);
85 RETVM_IF(ret < CONTACTS_ERROR_NONE, ret, "ctsvc_end_trans() Fail(%d)", ret);
87 return CONTACTS_ERROR_NONE;
90 static int __ctsvc_db_activity_photo_get_record(int id, contacts_record_h *out_record)
94 char query[CTS_SQL_MAX_LEN] = {0};
96 RETV_IF(NULL == out_record, CONTACTS_ERROR_INVALID_PARAMETER);
99 snprintf(query, sizeof(query),
100 "SELECT id, activity_id, photo_url, sort_index "
101 "FROM "CTSVC_DB_VIEW_ACTIVITY_PHOTOS" "
105 ret = ctsvc_query_prepare(query, &stmt);
106 RETVM_IF(NULL == stmt, ret, "ctsvc_query_prepare() Fail(%d)", ret);
108 ret = ctsvc_stmt_step(stmt);
109 if (1 /*CTS_TRUE*/ != ret) {
110 /* LCOV_EXCL_START */
111 ERR("ctsvc_stmt_step() Fail(%d)", ret);
112 ctsvc_stmt_finalize(stmt);
113 if (CONTACTS_ERROR_NONE == ret)
114 return CONTACTS_ERROR_NO_DATA;
120 ctsvc_db_activity_photo_get_value_from_stmt(stmt, out_record);
122 ctsvc_stmt_finalize(stmt);
124 return CONTACTS_ERROR_NONE;
127 static int __ctsvc_db_activity_photo_update_record(contacts_record_h record)
131 char query[CTS_SQL_MAX_LEN] = {0};
132 ctsvc_activity_photo_s *activity_photo = (ctsvc_activity_photo_s*)record;
134 RETVM_IF(NULL == activity_photo->photo_url, CONTACTS_ERROR_INVALID_PARAMETER, "photo_url is empty");
136 ret = ctsvc_begin_trans();
137 if (CONTACTS_ERROR_NONE != ret) {
138 /* LCOV_EXCL_START */
139 ERR("ctsvc_begin_trans() Fail(%d)", ret);
144 snprintf(query, sizeof(query),
145 "SELECT addressbook_id from "CTSVC_DB_VIEW_CONTACT" "
146 "WHERE contact_id = (SELECT contact_id from "CTS_TABLE_ACTIVITIES" WHERE id = %d)",
147 activity_photo->activity_id);
149 ret = ctsvc_query_get_first_int_result(query, &addressbook_id);
150 if (CONTACTS_ERROR_NONE != ret) {
151 /* LCOV_EXCL_START */
152 ERR("No data : activity_id (%d) is not exist", activity_photo->activity_id);
153 ctsvc_end_trans(false);
158 if (false == ctsvc_have_ab_write_permission(addressbook_id, false)) {
159 /* LCOV_EXCL_START */
160 ERR("No permission in this addresbook_id(%d)", addressbook_id);
161 ctsvc_end_trans(false);
162 return CONTACTS_ERROR_PERMISSION_DENIED;
166 ret = ctsvc_db_activity_photo_update(record);
167 if (CONTACTS_ERROR_NONE != ret) {
168 /* LCOV_EXCL_START */
169 ERR("Update record Fail(%d)", ret);
170 ctsvc_end_trans(false);
175 ret = ctsvc_end_trans(true);
176 RETVM_IF(ret < CONTACTS_ERROR_NONE, ret, "ctsvc_end_trans() Fail(%d)", ret);
178 return CONTACTS_ERROR_NONE;
181 static int __ctsvc_db_activity_photo_delete_record(int id)
184 char query[CTS_SQL_MAX_LEN] = {0};
187 ret = ctsvc_begin_trans();
188 if (CONTACTS_ERROR_NONE != ret) {
189 /* LCOV_EXCL_START */
190 ERR("ctsvc_begin_trans() Fail(%d)", ret);
195 snprintf(query, sizeof(query),
196 "SELECT C.addressbook_id FROM "CTSVC_DB_VIEW_CONTACT" C, "CTSVC_DB_VIEW_ACTIVITY" A, "CTSVC_DB_VIEW_ACTIVITY_PHOTOS" P "
197 "ON C.contact_id = A.contact_id AND A.id = P.activity_id "
198 "WHERE P.id = %d", id);
200 ret = ctsvc_query_get_first_int_result(query, &addressbook_id);
201 if (CONTACTS_ERROR_NONE != ret) {
202 /* LCOV_EXCL_START */
203 ERR("No data : id (%d) is not exist", id);
204 ctsvc_end_trans(false);
209 if (false == ctsvc_have_ab_write_permission(addressbook_id, false)) {
210 /* LCOV_EXCL_START */
211 ERR("No permission in this addresbook_id(%d)", addressbook_id);
212 ctsvc_end_trans(false);
213 return CONTACTS_ERROR_PERMISSION_DENIED;
217 ret = ctsvc_db_activity_photo_delete(id);
218 if (CONTACTS_ERROR_NONE != ret) {
219 /* LCOV_EXCL_START */
220 ERR("ctsvc_db_activity_photo_delete() Fail(%d)", ret);
221 ctsvc_end_trans(false);
226 ret = ctsvc_end_trans(true);
227 RETVM_IF(ret < CONTACTS_ERROR_NONE, ret, "ctsvc_end_trans() Fail(%d)", ret);
229 return CONTACTS_ERROR_NONE;
232 static int __ctsvc_db_activity_photo_get_all_records(int offset, int limit, contacts_list_h *out_list)
236 contacts_list_h list;
237 ctsvc_activity_photo_s *activity_photo;
238 cts_stmt stmt = NULL;
239 char query[CTS_SQL_MAX_LEN] = {0};
241 len = snprintf(query, sizeof(query),
242 "SELECT P.id, P.activity_id, P.photo_url, P.sort_index "
243 "FROM "CTSVC_DB_VIEW_CONTACT" C, "CTSVC_DB_VIEW_ACTIVITY" A, "CTSVC_DB_VIEW_ACTIVITY_PHOTOS" P "
244 "ON C.contact_id = A.contact_id AND A.id = P.activity_id ");
247 len += snprintf(query+len, sizeof(query)-len, " LIMIT %d", limit);
249 len += snprintf(query+len, sizeof(query)-len, " OFFSET %d", offset);
252 ret = ctsvc_query_prepare(query, &stmt);
253 RETVM_IF(NULL == stmt, ret, "ctsvc_query_prepare() Fail(%d)", ret);
255 contacts_list_create(&list);
256 while ((ret = ctsvc_stmt_step(stmt))) {
257 if (1 /*CTS_TRUE */ != ret) {
258 /* LCOV_EXCL_START */
259 ERR("DB : ctsvc_stmt_step fail(%d)", ret);
260 ctsvc_stmt_finalize(stmt);
261 contacts_list_destroy(list, true);
265 ctsvc_db_activity_photo_get_value_from_stmt(stmt, (contacts_record_h*)&activity_photo);
266 ctsvc_list_prepend(list, (contacts_record_h)activity_photo);
268 ctsvc_stmt_finalize(stmt);
269 ctsvc_list_reverse(list);
272 return CONTACTS_ERROR_NONE;
275 static int __ctsvc_db_activity_photo_get_records_with_query(contacts_query_h query, int offset,
276 int limit, contacts_list_h *out_list)
281 ctsvc_query_s *s_query;
283 contacts_list_h list;
284 ctsvc_activity_photo_s *activity_photo;
286 RETV_IF(NULL == query, CONTACTS_ERROR_INVALID_PARAMETER);
287 s_query = (ctsvc_query_s*)query;
289 ret = ctsvc_db_make_get_records_query_stmt(s_query, offset, limit, &stmt);
290 RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_db_make_get_records_query_stmt fail(%d)", ret);
292 contacts_list_create(&list);
293 while ((ret = ctsvc_stmt_step(stmt))) {
294 contacts_record_h record;
295 if (1 /*CTS_TRUE */ != ret) {
296 /* LCOV_EXCL_START */
297 ERR("ctsvc_stmt_step() Fail(%d)", ret);
298 ctsvc_stmt_finalize(stmt);
299 contacts_list_destroy(list, true);
304 contacts_record_create(_contacts_activity_photo._uri, &record);
305 activity_photo = (ctsvc_activity_photo_s*)record;
306 if (0 == s_query->projection_count) {
307 field_count = s_query->property_count;
309 field_count = s_query->projection_count;
310 ret = ctsvc_record_set_projection_flags(record, s_query->projection,
311 s_query->projection_count, s_query->property_count);
313 if (CONTACTS_ERROR_NONE != ret)
314 ASSERT_NOT_REACHED("To set projection is Fail\n");
317 for (i = 0; i < field_count; i++) {
320 if (0 == s_query->projection_count)
321 property_id = s_query->properties[i].property_id;
323 property_id = s_query->projection[i];
325 switch (property_id) {
326 case CTSVC_PROPERTY_ACTIVITY_PHOTO_ID:
327 activity_photo->id = ctsvc_stmt_get_int(stmt, i);
329 case CTSVC_PROPERTY_ACTIVITY_PHOTO_ACTIVITY_ID:
330 activity_photo->activity_id = ctsvc_stmt_get_int(stmt, i);
332 case CTSVC_PROPERTY_ACTIVITY_PHOTO_URL:
333 temp = ctsvc_stmt_get_text(stmt, i);
334 free(activity_photo->photo_url);
335 activity_photo->photo_url = SAFE_STRDUP(temp);
337 case CTSVC_PROPERTY_ACTIVITY_PHOTO_SORT_INDEX:
338 activity_photo->sort_index = ctsvc_stmt_get_int(stmt, i);
344 ctsvc_list_prepend(list, record);
346 ctsvc_stmt_finalize(stmt);
347 ctsvc_list_reverse(list);
350 return CONTACTS_ERROR_NONE;
353 ctsvc_db_plugin_info_s ctsvc_db_plugin_activity_photo = {
354 .is_query_only = false,
355 .insert_record = __ctsvc_db_activity_photo_insert_record,
356 .get_record = __ctsvc_db_activity_photo_get_record,
357 .update_record = __ctsvc_db_activity_photo_update_record,
358 .delete_record = __ctsvc_db_activity_photo_delete_record,
359 .get_all_records = __ctsvc_db_activity_photo_get_all_records,
360 .get_records_with_query = __ctsvc_db_activity_photo_get_records_with_query,
361 .insert_records = NULL,
362 .update_records = NULL,
363 .delete_records = NULL,
365 .get_count_with_query = NULL,
366 .replace_record = NULL,
367 .replace_records = NULL,