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