Sync with tizen 2.4
[platform/core/messaging/msg-service.git] / framework / storage-handler / MsgStorageFilter.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15 */
16
17 #include "MsgDebug.h"
18 #include "MsgSqliteWrapper.h"
19 #include "MsgStorageHandler.h"
20
21
22 /*==================================================================================================
23                                      VARIABLES
24 ==================================================================================================*/
25
26
27 /*==================================================================================================
28                                      FUNCTION IMPLEMENTATION
29 ==================================================================================================*/
30 msg_error_t MsgStoCheckDuplicatedFilter(const MSG_FILTER_S *pFilter)
31 {
32         MSG_BEGIN();
33         MsgDbHandler *dbHandle = getDbHandle();
34         msg_error_t err = MSG_SUCCESS;
35
36         char *filterStr = NULL;
37         char sqlQuery[MAX_QUERY_LEN+1];
38
39         MsgConvertStrWithEscape(pFilter->filterValue, &filterStr);
40
41         memset(sqlQuery, 0x00, sizeof(sqlQuery));
42         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT FILTER_ID FROM %s WHERE FILTER_TYPE = %d AND FILTER_VALUE = ?;",
43                         MSGFW_FILTER_TABLE_NAME, pFilter->filterType);
44
45         MSG_DEBUG("sql : %s", sqlQuery);
46
47         if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
48         {
49                 if (filterStr)
50                         free(filterStr);
51                 return MSG_ERR_DB_EXEC;
52         }
53
54         dbHandle->bindText(filterStr, 1);
55
56         err = dbHandle->stepQuery();
57
58         if (err == MSG_ERR_DB_ROW) {
59                 err = MSG_ERR_FILTER_DUPLICATED;
60         } else if (err == MSG_ERR_DB_DONE) {
61                 err = MSG_SUCCESS;
62         }
63
64         dbHandle->finalizeQuery();
65
66         if (filterStr)
67                 free(filterStr);
68
69         MSG_END();
70
71         return err;
72 }
73
74
75 msg_error_t MsgStoAddFilter(const MSG_FILTER_S *pFilter)
76 {
77         MSG_BEGIN();
78         MsgDbHandler *dbHandle = getDbHandle();
79         msg_error_t err = MSG_SUCCESS;
80
81         //check duplication
82         err = MsgStoCheckDuplicatedFilter(pFilter);
83
84         if (err != MSG_SUCCESS) {
85                 MSG_DEBUG("Filter is duplicated : [%d]", err);
86                 return err;
87         }
88
89         unsigned int rowId = 0;
90
91         char sqlQuery[MAX_QUERY_LEN+1];
92
93         err = dbHandle->getRowId(MSGFW_FILTER_TABLE_NAME, &rowId);
94
95         if (err != MSG_SUCCESS)
96                 return err;
97
98         // Add Filter
99         memset(sqlQuery, 0x00, sizeof(sqlQuery));
100
101         snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, ?, 1);",
102                         MSGFW_FILTER_TABLE_NAME, rowId, pFilter->filterType);
103
104         MSG_DEBUG("sql : %s", sqlQuery);
105
106         if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
107                 return MSG_ERR_DB_EXEC;
108
109         dbHandle->bindText(pFilter->filterValue, 1);
110
111         if (dbHandle->stepQuery() != MSG_ERR_DB_DONE) {
112                 dbHandle->finalizeQuery();
113                 return MSG_ERR_DB_EXEC;
114         }
115
116         dbHandle->finalizeQuery();
117
118         MSG_END();
119
120         return MSG_SUCCESS;
121 }
122
123
124 msg_error_t MsgStoUpdateFilter(const MSG_FILTER_S *pFilter)
125 {
126         MSG_BEGIN();
127         MsgDbHandler *dbHandle = getDbHandle();
128         msg_error_t err = MSG_SUCCESS;
129
130         //check duplication
131         err = MsgStoCheckDuplicatedFilter(pFilter);
132
133         if (err != MSG_SUCCESS) {
134                 MSG_DEBUG("Filter is duplicated : [%d]", err);
135                 return err;
136         }
137
138         char sqlQuery[MAX_QUERY_LEN+1];
139
140         // Update Filter
141         memset(sqlQuery, 0x00, sizeof(sqlQuery));
142
143         snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET FILTER_TYPE = %d, FILTER_VALUE = ? WHERE FILTER_ID = %d;",
144                         MSGFW_FILTER_TABLE_NAME, pFilter->filterType, pFilter->filterId);
145
146         if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
147                 return MSG_ERR_DB_PREPARE;
148
149         dbHandle->bindText(pFilter->filterValue, 1);
150
151         if (dbHandle->stepQuery() != MSG_ERR_DB_DONE) {
152                 dbHandle->finalizeQuery();
153                 return MSG_ERR_DB_EXEC;
154         }
155
156         dbHandle->finalizeQuery();
157
158         MSG_END();
159
160         return MSG_SUCCESS;
161 }
162
163
164 msg_error_t MsgStoDeleteFilter(msg_filter_id_t filterId)
165 {
166         MSG_BEGIN();
167
168         char sqlQuery[MAX_QUERY_LEN+1];
169         MsgDbHandler *dbHandle = getDbHandle();
170         dbHandle->beginTrans();
171
172         memset(sqlQuery, 0x00, sizeof(sqlQuery));
173
174         snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE FILTER_ID = %d;", MSGFW_FILTER_TABLE_NAME, filterId);
175
176         // Delete Filter
177         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
178                 dbHandle->endTrans(false);
179                 return MSG_ERR_DB_EXEC;
180         }
181
182         dbHandle->endTrans(true);
183
184         MSG_END();
185
186         return MSG_SUCCESS;
187 }
188
189
190 msg_error_t MsgStoGetFilterList(msg_struct_list_s *pFilterList)
191 {
192         MSG_BEGIN();
193
194         if (pFilterList == NULL) {
195                 MSG_DEBUG("pFilterList is NULL");
196                 return MSG_ERR_NULL_POINTER;
197         }
198         MsgDbHandler *dbHandle = getDbHandle();
199         int rowCnt = 0, index = 4;
200
201         char sqlQuery[MAX_QUERY_LEN+1];
202
203         // Get filters from DB
204         memset(sqlQuery, 0x00, sizeof(sqlQuery));
205
206         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT FILTER_ID, FILTER_TYPE, FILTER_VALUE, FILTER_ACTIVE FROM %s;", MSGFW_FILTER_TABLE_NAME);
207
208         msg_error_t err = MSG_SUCCESS;
209
210         err = dbHandle->getTable(sqlQuery, &rowCnt);
211
212         if (err == MSG_ERR_DB_NORECORD) {
213                 pFilterList->nCount = 0;
214                 pFilterList->msg_struct_info = NULL;
215
216                 dbHandle->freeTable();
217
218                 return MSG_SUCCESS;
219         } else if (err != MSG_SUCCESS) {
220                 dbHandle->freeTable();
221                 return err;
222         }
223
224         pFilterList->nCount = rowCnt;
225
226         MSG_DEBUG("pMsgCommInfoList->nCount [%d]", pFilterList->nCount);
227
228         pFilterList->msg_struct_info = (msg_struct_t *)calloc(rowCnt, sizeof(MSG_FILTER_S *));
229
230         msg_struct_s* pTmp = NULL;
231
232         for (int i = 0; i < rowCnt; i++)
233         {
234                 pFilterList->msg_struct_info[i] = (msg_struct_t)new msg_struct_s;
235
236                 pTmp = (msg_struct_s *)pFilterList->msg_struct_info[i];
237                 pTmp->type = MSG_STRUCT_FILTER;
238                 pTmp->data = new MSG_FILTER_S;
239                 MSG_FILTER_S *pFilter = (MSG_FILTER_S *)pTmp->data;
240                 memset(pFilter, 0x00, sizeof(MSG_FILTER_S));
241                 pFilter->filterId = dbHandle->getColumnToInt(index++);
242                 pFilter->filterType = dbHandle->getColumnToInt(index++);
243                 memset(pFilter->filterValue, 0x00, sizeof(pFilter->filterValue));
244                 dbHandle->getColumnToString(index++, MAX_FILTER_VALUE_LEN, pFilter->filterValue);
245                 pFilter->bActive = dbHandle->getColumnToInt(index++);
246         }
247
248
249         dbHandle->freeTable();
250
251         MSG_END();
252
253         return MSG_SUCCESS;
254 }
255
256
257 msg_error_t MsgStoSetFilterActivation(msg_filter_id_t filterId, bool bActive)
258 {
259         MSG_BEGIN();
260         MsgDbHandler *dbHandle = getDbHandle();
261         char sqlQuery[MAX_QUERY_LEN+1];
262
263         // Set Filter Activation
264         memset(sqlQuery, 0x00, sizeof(sqlQuery));
265
266         snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET FILTER_ACTIVE = %d WHERE FILTER_ID = %d;",
267                         MSGFW_FILTER_TABLE_NAME, bActive, filterId);
268
269         MSG_DEBUG("sqlQuery [%s]", sqlQuery);
270
271         if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
272                 return MSG_ERR_DB_EXEC;
273         }
274
275         MSG_END();
276
277         return MSG_SUCCESS;
278 }