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