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