[SVACE Issue Fixes]
[platform/core/pim/contacts-service.git] / server / db / ctsvc_db_plugin_activity_photo_helper.c
1 /*
2  * Contacts Service
3  *
4  * Copyright (c) 2010 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19 #include "contacts.h"
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"
29
30
31 int ctsvc_db_activity_photo_get_value_from_stmt(cts_stmt stmt, contacts_record_h *record)
32 {
33         int ret;
34         char *temp;
35         ctsvc_activity_photo_s *activity_photo;
36         int start_count = 0;
37
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);
40
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++);
46
47         *record = (contacts_record_h)activity_photo;
48         return CONTACTS_ERROR_NONE;
49 }
50
51 int ctsvc_db_activity_photo_insert(contacts_record_h record, int activity_id, int *id)
52 {
53         int ret;
54         cts_stmt stmt = NULL;
55         ctsvc_activity_photo_s *activity_photo = (ctsvc_activity_photo_s*)record;
56         char query[CTS_SQL_MAX_LEN] = {0};
57
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);
63
64         snprintf(query, sizeof(query),
65                         "INSERT INTO "CTS_TABLE_ACTIVITY_PHOTOS"(activity_id, photo_url, sort_index) "
66                         "VALUES(%d, ?, %d)",
67                         activity_id, activity_photo->sort_index);
68
69         ret = ctsvc_query_prepare(query, &stmt);
70         RETVM_IF(NULL == stmt, ret, "ctsvc_query_prepare() Fail(%d)", ret);
71
72         sqlite3_bind_text(stmt, 1, activity_photo->photo_url, strlen(activity_photo->photo_url), SQLITE_STATIC);
73
74         ret = ctsvc_stmt_step(stmt);
75         if (CONTACTS_ERROR_NONE != ret) {
76                 /* LCOV_EXCL_START */
77                 ERR("ctsvc_stmt_step() Fail(%d)", ret);
78                 ctsvc_stmt_finalize(stmt);
79                 return ret;
80                 /* LCOV_EXCL_STOP */
81         }
82         if (id)
83                 *id = ctsvc_db_get_last_insert_id();
84         ctsvc_stmt_finalize(stmt);
85
86         ctsvc_set_activity_noti();
87         ctsvc_set_activity_photo_noti();
88
89         return CONTACTS_ERROR_NONE;
90 }
91
92 int ctsvc_db_activity_photo_update(contacts_record_h record)
93 {
94         int id;
95         int ret = CONTACTS_ERROR_NONE;
96         char *set = NULL;
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};
101
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");
104
105         snprintf(query, sizeof(query),
106                         "SELECT id FROM "CTS_TABLE_ACTIVITY_PHOTOS" WHERE id = %d", activity_photo->id);
107
108         ret = ctsvc_query_get_first_int_result(query, &id);
109         RETV_IF(ret != CONTACTS_ERROR_NONE, ret);
110
111         do {
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();
116         } while (0);
117
118         CTSVC_RECORD_RESET_PROPERTY_FLAGS((ctsvc_record_s*)record);
119         free(set);
120
121         if (bind_text) {
122                 for (cursor = bind_text; cursor; cursor = cursor->next) {
123                         free(cursor->data);
124                         cursor->data = NULL;
125                 }
126                 g_slist_free(bind_text);
127         }
128
129         return ret;
130 }
131
132 int ctsvc_db_activity_photo_delete(int id)
133 {
134         int ret;
135         char query[CTS_SQL_MIN_LEN] = {0};
136
137         snprintf(query, sizeof(query), "DELETE FROM "CTS_TABLE_ACTIVITY_PHOTOS" WHERE id = %d", id);
138
139         ret = ctsvc_query_exec(query);
140         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_query_exec() Fail(%d)", ret);
141
142         ctsvc_set_activity_noti();
143         ctsvc_set_activity_photo_noti();
144
145         return CONTACTS_ERROR_NONE;
146 }
147
148
149 int ctsvc_db_activity_photo_get_records(int activity_id, contacts_record_h record_activity)
150 {
151         char query[CTS_SQL_MAX_LEN] = {0};
152         int ret;
153         cts_stmt stmt = NULL;
154         contacts_list_h list;
155
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);
159
160         ret = ctsvc_query_prepare(query, &stmt);
161         RETVM_IF(NULL == stmt, ret, "ctsvc_query_prepare() Fail(%d)", ret);
162
163         contacts_list_create(&list);
164         while ((ret = ctsvc_stmt_step(stmt))) {
165                 if (1 != ret) {
166                         /* LCOV_EXCL_START */
167                         ERR("ctsvc_stmt_step() Fail(%d)", ret);
168                         ctsvc_stmt_finalize(stmt);
169                         contacts_list_destroy(list, true);
170                         return ret;
171                         /* LCOV_EXCL_STOP */
172                 }
173                 contacts_record_h record = NULL;
174                 ctsvc_db_activity_photo_get_value_from_stmt(stmt, &record);
175                 ctsvc_list_prepend(list, record);
176         }
177
178         ctsvc_stmt_finalize(stmt);
179
180         ((ctsvc_activity_s*)record_activity)->photos = (ctsvc_list_s*)list;
181
182         return CONTACTS_ERROR_NONE;
183 }
184
185
186
187