2 * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "MsgSqliteWrapper.h"
19 #include "MsgStorageHandler.h"
22 /*==================================================================================================
24 ==================================================================================================*/
27 /*==================================================================================================
28 FUNCTION IMPLEMENTATION
29 ==================================================================================================*/
30 msg_error_t MsgStoCheckDuplicatedFilter(const MSG_FILTER_S *pFilter)
33 MsgDbHandler *dbHandle = getDbHandle();
34 msg_error_t err = MSG_SUCCESS;
36 char *filterStr = NULL;
37 char sqlQuery[MAX_QUERY_LEN+1];
39 MsgConvertStrWithEscape(pFilter->filterValue, &filterStr);
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);
45 MSG_DEBUG("sql : %s", sqlQuery);
47 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
51 return MSG_ERR_DB_EXEC;
54 dbHandle->bindText(filterStr, 1);
56 err = dbHandle->stepQuery();
58 if (err == MSG_ERR_DB_ROW) {
59 err = MSG_ERR_FILTER_DUPLICATED;
60 } else if (err == MSG_ERR_DB_DONE) {
64 dbHandle->finalizeQuery();
75 msg_error_t MsgStoAddFilter(const MSG_FILTER_S *pFilter)
78 MsgDbHandler *dbHandle = getDbHandle();
79 msg_error_t err = MSG_SUCCESS;
82 err = MsgStoCheckDuplicatedFilter(pFilter);
84 if (err != MSG_SUCCESS) {
85 MSG_DEBUG("Filter is duplicated : [%d]", err);
89 unsigned int rowId = 0;
91 char sqlQuery[MAX_QUERY_LEN+1];
93 err = dbHandle->getRowId(MSGFW_FILTER_TABLE_NAME, &rowId);
95 if (err != MSG_SUCCESS)
99 memset(sqlQuery, 0x00, sizeof(sqlQuery));
101 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, %d, ?, 1);",
102 MSGFW_FILTER_TABLE_NAME, rowId, pFilter->filterType);
104 MSG_DEBUG("sql : %s", sqlQuery);
106 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
107 return MSG_ERR_DB_EXEC;
109 dbHandle->bindText(pFilter->filterValue, 1);
111 if (dbHandle->stepQuery() != MSG_ERR_DB_DONE) {
112 dbHandle->finalizeQuery();
113 return MSG_ERR_DB_EXEC;
116 dbHandle->finalizeQuery();
124 msg_error_t MsgStoUpdateFilter(const MSG_FILTER_S *pFilter)
127 MsgDbHandler *dbHandle = getDbHandle();
128 msg_error_t err = MSG_SUCCESS;
131 err = MsgStoCheckDuplicatedFilter(pFilter);
133 if (err != MSG_SUCCESS) {
134 MSG_DEBUG("Filter is duplicated : [%d]", err);
138 char sqlQuery[MAX_QUERY_LEN+1];
141 memset(sqlQuery, 0x00, sizeof(sqlQuery));
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);
146 if (dbHandle->prepareQuery(sqlQuery) != MSG_SUCCESS)
147 return MSG_ERR_DB_PREPARE;
149 dbHandle->bindText(pFilter->filterValue, 1);
151 if (dbHandle->stepQuery() != MSG_ERR_DB_DONE) {
152 dbHandle->finalizeQuery();
153 return MSG_ERR_DB_EXEC;
156 dbHandle->finalizeQuery();
164 msg_error_t MsgStoDeleteFilter(msg_filter_id_t filterId)
168 char sqlQuery[MAX_QUERY_LEN+1];
169 MsgDbHandler *dbHandle = getDbHandle();
170 dbHandle->beginTrans();
172 memset(sqlQuery, 0x00, sizeof(sqlQuery));
174 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE FILTER_ID = %d;", MSGFW_FILTER_TABLE_NAME, filterId);
177 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
178 dbHandle->endTrans(false);
179 return MSG_ERR_DB_EXEC;
182 dbHandle->endTrans(true);
190 msg_error_t MsgStoGetFilterList(msg_struct_list_s *pFilterList)
194 if (pFilterList == NULL) {
195 MSG_DEBUG("pFilterList is NULL");
196 return MSG_ERR_NULL_POINTER;
198 MsgDbHandler *dbHandle = getDbHandle();
199 int rowCnt = 0, index = 4;
201 char sqlQuery[MAX_QUERY_LEN+1];
203 // Get filters from DB
204 memset(sqlQuery, 0x00, sizeof(sqlQuery));
206 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT FILTER_ID, FILTER_TYPE, FILTER_VALUE, FILTER_ACTIVE FROM %s;", MSGFW_FILTER_TABLE_NAME);
208 msg_error_t err = MSG_SUCCESS;
210 err = dbHandle->getTable(sqlQuery, &rowCnt);
212 if (err == MSG_ERR_DB_NORECORD) {
213 pFilterList->nCount = 0;
214 pFilterList->msg_struct_info = NULL;
216 dbHandle->freeTable();
219 } else if (err != MSG_SUCCESS) {
220 dbHandle->freeTable();
224 pFilterList->nCount = rowCnt;
226 MSG_DEBUG("pMsgCommInfoList->nCount [%d]", pFilterList->nCount);
228 pFilterList->msg_struct_info = (msg_struct_t *)calloc(rowCnt, sizeof(MSG_FILTER_S *));
230 msg_struct_s* pTmp = NULL;
232 for (int i = 0; i < rowCnt; i++)
234 pFilterList->msg_struct_info[i] = (msg_struct_t)new msg_struct_s;
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++);
249 dbHandle->freeTable();
257 msg_error_t MsgStoSetFilterActivation(msg_filter_id_t filterId, bool bActive)
260 MsgDbHandler *dbHandle = getDbHandle();
261 char sqlQuery[MAX_QUERY_LEN+1];
263 // Set Filter Activation
264 memset(sqlQuery, 0x00, sizeof(sqlQuery));
266 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET FILTER_ACTIVE = %d WHERE FILTER_ID = %d;",
267 MSGFW_FILTER_TABLE_NAME, bActive, filterId);
269 MSG_DEBUG("sqlQuery [%s]", sqlQuery);
271 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
272 return MSG_ERR_DB_EXEC;