[SVACE Issue Fixes]
[platform/core/pim/contacts-service.git] / server / db / ctsvc_db_plugin_grouprelation.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_utils.h"
24 #include "ctsvc_list.h"
25 #include "ctsvc_db_init.h"
26 #include "ctsvc_db_query.h"
27 #include "ctsvc_record.h"
28 #include "ctsvc_db_access_control.h"
29
30
31 static int __ctsvc_db_grouprelation_insert_record(contacts_record_h record, int *id)
32 {
33         /* LCOV_EXCL_START */
34         ERR("Please use the contacts_group_add_contact()");
35         return CONTACTS_ERROR_INVALID_PARAMETER;
36         /* LCOV_EXCL_STOP */
37 }
38
39 static int __ctsvc_db_grouprelation_get_record(int id, contacts_record_h *out_record)
40 {
41         /* LCOV_EXCL_START */
42         ERR("Not support get group-relation");
43         return CONTACTS_ERROR_INVALID_PARAMETER;
44         /* LCOV_EXCL_STOP */
45 }
46
47 static int __ctsvc_db_grouprelation_update_record(contacts_record_h record)
48 {
49         /* LCOV_EXCL_START */
50         ERR("Not support update group-relation");
51         return CONTACTS_ERROR_INVALID_PARAMETER;
52         /* LCOV_EXCL_STOP */
53 }
54
55 static int __ctsvc_db_grouprelation_delete_record(int id)
56 {
57         /* LCOV_EXCL_START */
58         ERR("Please use the contacts_group_remove_contact()");
59         return CONTACTS_ERROR_INVALID_PARAMETER;
60         /* LCOV_EXCL_STOP */
61 }
62
63 static int __ctsvc_db_grouprelation_get_all_records(int offset, int limit, contacts_list_h *out_list)
64 {
65         int len;
66         int ret;
67         contacts_list_h list;
68         ctsvc_group_relation_s *grouprel;
69         cts_stmt stmt = NULL;
70         char query[CTS_SQL_MAX_LEN] = {0};
71         char *temp;
72
73         len = snprintf(query, sizeof(query),
74                         "SELECT group_id, contact_id, group_name FROM "CTSVC_DB_VIEW_GROUP_RELATION);
75
76         if (0 != limit) {
77                 len += snprintf(query+len, sizeof(query)-len, " LIMIT %d", limit);
78                 if (0 < offset)
79                         len += snprintf(query+len, sizeof(query)-len, " OFFSET %d", offset);
80         }
81
82         ret = ctsvc_query_prepare(query, &stmt);
83         RETVM_IF(NULL == stmt, ret, "ctsvc_query_prepare() Fail(%d)", ret);
84
85         contacts_list_create(&list);
86         while ((ret = ctsvc_stmt_step(stmt))) {
87                 if (1 != ret) {
88                         /* LCOV_EXCL_START */
89                         ERR("ctsvc_stmt_step() Fail(%d)", ret);
90                         ctsvc_stmt_finalize(stmt);
91                         contacts_list_destroy(list, true);
92                         return ret;
93                         /* LCOV_EXCL_STOP */
94                 }
95                 contacts_record_create(_contacts_group_relation._uri, (contacts_record_h*)&grouprel);
96
97                 if (grouprel) {
98                         grouprel->group_id = ctsvc_stmt_get_int(stmt, 0);
99                         grouprel->id = grouprel->group_id;
100                         grouprel->contact_id = ctsvc_stmt_get_int(stmt, 1);
101                         temp = ctsvc_stmt_get_text(stmt, 2);
102                         grouprel->group_name = SAFE_STRDUP(temp);
103
104                         ctsvc_list_prepend(list, (contacts_record_h)grouprel);
105                 }
106         }
107
108         ctsvc_stmt_finalize(stmt);
109         ctsvc_list_reverse(list);
110
111         *out_list = list;
112         return CONTACTS_ERROR_NONE;
113 }
114
115 static int __ctsvc_db_grouprelation_get_records_with_query(contacts_query_h query,
116                 int offset, int limit, contacts_list_h *out_list)
117 {
118         int ret;
119         int i;
120         int field_count;
121         ctsvc_query_s *s_query;
122         cts_stmt stmt;
123         contacts_list_h list;
124         ctsvc_group_relation_s *group_relation;
125
126         RETV_IF(NULL == query, CONTACTS_ERROR_INVALID_PARAMETER);
127         s_query = (ctsvc_query_s*)query;
128
129         ret = ctsvc_db_make_get_records_query_stmt(s_query, offset, limit, &stmt);
130         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_db_make_get_records_query_stmt fail(%d)", ret);
131
132         contacts_list_create(&list);
133         while ((ret = ctsvc_stmt_step(stmt))) {
134                 contacts_record_h record;
135                 if (1 != ret) {
136                         /* LCOV_EXCL_START */
137                         ERR("ctsvc_stmt_step() Fail(%d)", ret);
138                         ctsvc_stmt_finalize(stmt);
139                         contacts_list_destroy(list, true);
140                         return ret;
141                         /* LCOV_EXCL_STOP */
142                 }
143
144                 contacts_record_create(_contacts_group_relation._uri, &record);
145                 group_relation = (ctsvc_group_relation_s*)record;
146                 if (0 == s_query->projection_count)
147                         field_count = s_query->property_count;
148                 else {
149                         field_count = s_query->projection_count;
150
151                         int err = ctsvc_record_set_projection_flags(record, s_query->projection,
152                                         s_query->projection_count, s_query->property_count);
153                         if (CONTACTS_ERROR_NONE != err)
154                                 ASSERT_NOT_REACHED("To set projection is Fail.\n");
155                 }
156
157                 for (i = 0; i < field_count; i++) {
158                         char *temp;
159                         int property_id;
160                         if (0 == s_query->projection_count)
161                                 property_id = s_query->properties[i].property_id;
162                         else
163                                 property_id = s_query->projection[i];
164
165                         if (CTSVC_PROPERTY_GROUP_RELATION_ID == property_id)
166                                 continue;
167
168                         switch (property_id) {
169                         case CTSVC_PROPERTY_GROUP_RELATION_CONTACT_ID:
170                                 group_relation->contact_id = ctsvc_stmt_get_int(stmt, i);
171                                 break;
172                         case CTSVC_PROPERTY_GROUP_RELATION_GROUP_ID:
173                                 group_relation->group_id = ctsvc_stmt_get_int(stmt, i);
174                                 group_relation->id = group_relation->group_id;
175                                 break;
176                         case CTSVC_PROPERTY_GROUP_RELATION_GROUP_NAME:
177                                 temp = ctsvc_stmt_get_text(stmt, i);
178                                 free(group_relation->group_name);
179                                 group_relation->group_name = SAFE_STRDUP(temp);
180                                 break;
181                         default:
182                                 break;
183                         }
184                 }
185                 ctsvc_list_prepend(list, record);
186         }
187         ctsvc_stmt_finalize(stmt);
188         ctsvc_list_reverse(list);
189
190         *out_list = list;
191         return CONTACTS_ERROR_NONE;
192 }
193
194 ctsvc_db_plugin_info_s ctsvc_db_plugin_grouprelation = {
195         .is_query_only = false,
196         .insert_record = __ctsvc_db_grouprelation_insert_record,
197         .get_record = __ctsvc_db_grouprelation_get_record,
198         .update_record = __ctsvc_db_grouprelation_update_record,
199         .delete_record = __ctsvc_db_grouprelation_delete_record,
200         .get_all_records = __ctsvc_db_grouprelation_get_all_records,
201         .get_records_with_query = __ctsvc_db_grouprelation_get_records_with_query,
202         .insert_records = NULL,
203         .update_records = NULL,
204         .delete_records = NULL,
205         .get_count = NULL,
206         .get_count_with_query = NULL,
207         .replace_record = NULL,
208         .replace_records = NULL,
209 };
210