[SVACE Issue Fixes]
[platform/core/pim/contacts-service.git] / server / db / ctsvc_db_plugin_relationship_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_relationship_helper.h"
26 #include "ctsvc_record.h"
27 #include "ctsvc_notification.h"
28
29
30 int ctsvc_db_relationship_get_value_from_stmt(cts_stmt stmt, contacts_record_h *record, int start_count)
31 {
32         int ret;
33         char *temp;
34         ctsvc_relationship_s *relationship;
35
36         ret = contacts_record_create(_contacts_relationship._uri, (contacts_record_h*)&relationship);
37         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "contacts_record_create Fail(%d)", ret);
38
39         relationship->id = ctsvc_stmt_get_int(stmt, start_count++);
40         relationship->contact_id = ctsvc_stmt_get_int(stmt, start_count++);
41         start_count++;
42         relationship->type = ctsvc_stmt_get_int(stmt, start_count++);
43         temp = ctsvc_stmt_get_text(stmt, start_count++);
44         relationship->label = SAFE_STRDUP(temp);
45         temp = ctsvc_stmt_get_text(stmt, start_count++);
46         relationship->name = SAFE_STRDUP(temp);
47
48         *record = (contacts_record_h)relationship;
49         return CONTACTS_ERROR_NONE;
50 }
51
52 static inline int __ctsvc_relationship_bind_stmt(cts_stmt stmt, ctsvc_relationship_s *relationship, int start_cnt)
53 {
54         if (relationship->label)
55                 ctsvc_stmt_bind_text(stmt, start_cnt, relationship->label);
56         if (relationship->name)
57                 ctsvc_stmt_bind_text(stmt, 2, relationship->name);
58         return CONTACTS_ERROR_NONE;
59 }
60
61 int ctsvc_db_relationship_insert(contacts_record_h record, int contact_id, bool is_my_profile, int *id)
62 {
63         int ret;
64         cts_stmt stmt = NULL;
65         char query[CTS_SQL_MAX_LEN] = {0};
66         ctsvc_relationship_s *relationship = (ctsvc_relationship_s*)record;
67
68         /* These check should be done in client side */
69         RETV_IF(NULL == relationship->name, CONTACTS_ERROR_NONE);
70         RETVM_IF(contact_id <= 0, CONTACTS_ERROR_INVALID_PARAMETER,
71                         "contact_id(%d) is mandatory field to insert relationship record", relationship->contact_id);
72         RETVM_IF(0 < relationship->id, CONTACTS_ERROR_INVALID_PARAMETER,
73                         "id(%d), This record is already inserted", relationship->id);
74
75         snprintf(query, sizeof(query),
76                         "INSERT INTO "CTS_TABLE_DATA"(contact_id, is_my_profile, datatype, data1, data2, data3) "
77                         "VALUES(%d, %d, %d, %d, ?, ?)",
78                         contact_id, is_my_profile, CONTACTS_DATA_TYPE_RELATIONSHIP, relationship->type);
79
80         ret = ctsvc_query_prepare(query, &stmt);
81         RETVM_IF(NULL == stmt, ret, "ctsvc_query_prepare() Fail(%d)", ret);
82
83         __ctsvc_relationship_bind_stmt(stmt, relationship, 1);
84
85         ret = ctsvc_stmt_step(stmt);
86         if (CONTACTS_ERROR_NONE != ret) {
87                 /* LCOV_EXCL_START */
88                 ERR("ctsvc_stmt_step() Fail(%d)", ret);
89                 ctsvc_stmt_finalize(stmt);
90                 return ret;
91                 /* LCOV_EXCL_STOP */
92         }
93
94         /* relationship->id = ctsvc_db_get_last_insert_id(); */
95         if (id)
96                 *id = ctsvc_db_get_last_insert_id();
97         ctsvc_stmt_finalize(stmt);
98
99         if (false == is_my_profile)
100                 ctsvc_set_relationship_noti();
101
102         return CONTACTS_ERROR_NONE;
103 }
104
105 int ctsvc_db_relationship_update(contacts_record_h record, bool is_my_profile)
106 {
107         int id;
108         int ret = CONTACTS_ERROR_NONE;
109         char *set = NULL;
110         GSList *bind_text = NULL;
111         GSList *cursor = NULL;
112         ctsvc_relationship_s *relationship = (ctsvc_relationship_s*)record;
113         char query[CTS_SQL_MIN_LEN] = {0};
114
115         RETVM_IF(0 == relationship->id, CONTACTS_ERROR_INVALID_PARAMETER, "relationship of contact has no ID.");
116         snprintf(query, sizeof(query),
117                         "SELECT id FROM "CTS_TABLE_DATA" WHERE id = %d", relationship->id);
118         ret = ctsvc_query_get_first_int_result(query, &id);
119         RETV_IF(ret != CONTACTS_ERROR_NONE, ret);
120
121         RETVM_IF(CTSVC_PROPERTY_FLAG_DIRTY != (relationship->base.property_flag & CTSVC_PROPERTY_FLAG_DIRTY), CONTACTS_ERROR_NONE, "No update");
122
123         do {
124                 if (CONTACTS_ERROR_NONE != (ret = ctsvc_db_create_set_query(record, &set, &bind_text))) break;
125                 if (CONTACTS_ERROR_NONE != (ret = ctsvc_db_update_record_with_set_query(set, bind_text, CTS_TABLE_DATA, relationship->id))) break;
126                 if (false == is_my_profile)
127                         ctsvc_set_relationship_noti();
128         } while (0);
129
130         CTSVC_RECORD_RESET_PROPERTY_FLAGS((ctsvc_record_s*)record);
131         free(set);
132
133         if (bind_text) {
134                 for (cursor = bind_text; cursor; cursor = cursor->next) {
135                         free(cursor->data);
136                         cursor->data = NULL;
137                 }
138                 g_slist_free(bind_text);
139         }
140
141         return ret;
142 }
143
144
145 int ctsvc_db_relationship_delete(int id, bool is_my_profile)
146 {
147         int ret;
148         char query[CTS_SQL_MIN_LEN] = {0};
149
150         snprintf(query, sizeof(query), "DELETE FROM "CTS_TABLE_DATA" WHERE id = %d AND datatype = %d",
151                         id, CONTACTS_DATA_TYPE_RELATIONSHIP);
152
153         ret = ctsvc_query_exec(query);
154         RETVM_IF(CONTACTS_ERROR_NONE != ret, ret, "ctsvc_query_exec() Fail(%d)", ret);
155
156         if (false == is_my_profile)
157                 ctsvc_set_relationship_noti();
158
159         return CONTACTS_ERROR_NONE;
160 }
161