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_init.h"
24 #include "ctsvc_db_query.h"
25 #include "ctsvc_db_plugin_activity_photo_helper.h"
26 #include "ctsvc_record.h"
27 #include "ctsvc_list.h"
28 #include "ctsvc_notification.h"
31 int ctsvc_db_activity_photo_get_value_from_stmt(cts_stmt stmt, contacts_record_h *record)
35 ctsvc_activity_photo_s *activity_photo;
38 ret = contacts_record_create(_contacts_activity_photo._uri, (contacts_record_h*)&activity_photo);
39 RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "contacts_record_create Fail(%d)", ret);
41 activity_photo->id = ctsvc_stmt_get_int(stmt, start_count++);
42 activity_photo->activity_id = ctsvc_stmt_get_int(stmt, start_count++);
43 temp = ctsvc_stmt_get_text(stmt, start_count++);
44 activity_photo->photo_url = SAFE_STRDUP(temp);
45 activity_photo->sort_index = ctsvc_stmt_get_int(stmt, start_count++);
47 *record = (contacts_record_h)activity_photo;
48 return CONTACTS_ERROR_NONE;
51 int ctsvc_db_activity_photo_insert(contacts_record_h record, int activity_id, int *id)
55 ctsvc_activity_photo_s *activity_photo = (ctsvc_activity_photo_s*)record;
56 char query[CTS_SQL_MAX_LEN] = {0};
58 RETVM_IF(activity_id <= 0, CONTACTS_ERROR_INVALID_PARAMETER,
59 "activity_id(%d) Invalid", activity_id);
60 RETVM_IF(0 < activity_photo->id, CONTACTS_ERROR_INVALID_PARAMETER,
61 "id(%d), This record is already inserted", activity_photo->id);
62 RETV_IF(NULL == activity_photo->photo_url, CONTACTS_ERROR_INVALID_PARAMETER);
64 snprintf(query, sizeof(query),
65 "INSERT INTO "CTS_TABLE_ACTIVITY_PHOTOS"(activity_id, photo_url, sort_index) "
67 activity_id, activity_photo->sort_index);
69 ret = ctsvc_query_prepare(query, &stmt);
70 RETVM_IF(NULL == stmt, ret, "ctsvc_query_prepare() Fail(%d)", ret);
72 sqlite3_bind_text(stmt, 1, activity_photo->photo_url, strlen(activity_photo->photo_url), SQLITE_STATIC);
74 ret = ctsvc_stmt_step(stmt);
75 if (CONTACTS_ERROR_NONE != ret) {
77 ERR("ctsvc_stmt_step() Fail(%d)", ret);
78 ctsvc_stmt_finalize(stmt);
83 *id = ctsvc_db_get_last_insert_id();
84 ctsvc_stmt_finalize(stmt);
86 ctsvc_set_activity_noti();
87 ctsvc_set_activity_photo_noti();
89 return CONTACTS_ERROR_NONE;
92 int ctsvc_db_activity_photo_update(contacts_record_h record)
95 int ret = CONTACTS_ERROR_NONE;
97 GSList *bind_text = NULL;
98 GSList *cursor = NULL;
99 ctsvc_activity_photo_s *activity_photo = (ctsvc_activity_photo_s*)record;
100 char query[CTS_SQL_MIN_LEN] = {0};
102 RETVM_IF(0 == activity_photo->id, CONTACTS_ERROR_INVALID_PARAMETER, "activity_photo has no ID.");
103 RETVM_IF(CTSVC_PROPERTY_FLAG_DIRTY != (activity_photo->base.property_flag & CTSVC_PROPERTY_FLAG_DIRTY), CONTACTS_ERROR_NONE, "No update");
105 snprintf(query, sizeof(query),
106 "SELECT id FROM "CTS_TABLE_ACTIVITY_PHOTOS" WHERE id = %d", activity_photo->id);
108 ret = ctsvc_query_get_first_int_result(query, &id);
109 RETV_IF(ret != CONTACTS_ERROR_NONE, ret);
112 if (CONTACTS_ERROR_NONE != (ret = ctsvc_db_create_set_query(record, &set, &bind_text))) break;
113 if (CONTACTS_ERROR_NONE != (ret = ctsvc_db_update_record_with_set_query(set, bind_text, CTS_TABLE_ACTIVITY_PHOTOS, activity_photo->id))) break;
114 ctsvc_set_activity_noti();
115 ctsvc_set_activity_photo_noti();
118 CTSVC_RECORD_RESET_PROPERTY_FLAGS((ctsvc_record_s*)record);
122 for (cursor = bind_text; cursor; cursor = cursor->next) {
126 g_slist_free(bind_text);
132 int ctsvc_db_activity_photo_delete(int id)
135 char query[CTS_SQL_MIN_LEN] = {0};
137 snprintf(query, sizeof(query), "DELETE FROM "CTS_TABLE_ACTIVITY_PHOTOS" WHERE id = %d", id);
139 ret = ctsvc_query_exec(query);
140 RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_query_exec() Fail(%d)", ret);
142 ctsvc_set_activity_noti();
143 ctsvc_set_activity_photo_noti();
145 return CONTACTS_ERROR_NONE;
149 int ctsvc_db_activity_photo_get_records(int activity_id, contacts_record_h record_activity)
151 char query[CTS_SQL_MAX_LEN] = {0};
153 cts_stmt stmt = NULL;
154 contacts_list_h list;
156 snprintf(query, sizeof(query), "SELECT activity_id, activity_id, photo_url, sort_index "
157 "FROM "CTSVC_DB_VIEW_ACTIVITY_PHOTOS" WHERE activity_id = %d "
158 "ORDER BY sort_index ASC", activity_id);
160 ret = ctsvc_query_prepare(query, &stmt);
161 RETVM_IF(NULL == stmt, ret, "ctsvc_query_prepare() Fail(%d)", ret);
163 contacts_list_create(&list);
164 while ((ret = ctsvc_stmt_step(stmt))) {
166 /* LCOV_EXCL_START */
167 ERR("ctsvc_stmt_step() Fail(%d)", ret);
168 ctsvc_stmt_finalize(stmt);
169 contacts_list_destroy(list, true);
173 contacts_record_h record = NULL;
174 ctsvc_db_activity_photo_get_value_from_stmt(stmt, &record);
175 ctsvc_list_prepend(list, record);
178 ctsvc_stmt_finalize(stmt);
180 ((ctsvc_activity_s*)record_activity)->photos = (ctsvc_list_s*)list;
182 return CONTACTS_ERROR_NONE;