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 "MsgUtilStorage.h"
19 #include "MsgSqliteWrapper.h"
20 #include "MsgStorageHandler.h"
23 /*==================================================================================================
25 ==================================================================================================*/
27 /*==================================================================================================
28 FUNCTION IMPLEMENTATION
29 ==================================================================================================*/
30 msg_error_t MsgStoAddFolder(const MSG_FOLDER_INFO_S *pFolderInfo)
32 msg_error_t err = MSG_SUCCESS;
33 MsgDbHandler *dbHandle = getDbHandle();
34 unsigned int rowId = 0;
36 char sqlQuery[MAX_QUERY_LEN+1];
38 err = dbHandle->getRowId(MSGFW_FOLDER_TABLE_NAME, &rowId);
40 if (err != MSG_SUCCESS)
44 memset(sqlQuery, 0x00, sizeof(sqlQuery));
45 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, '%s', %d);",
46 MSGFW_FOLDER_TABLE_NAME, rowId, pFolderInfo->folderName, pFolderInfo->folderType);
48 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS)
49 return MSG_ERR_DB_EXEC;
55 msg_error_t MsgStoUpdateFolder(const MSG_FOLDER_INFO_S *pFolderInfo)
57 char sqlQuery[MAX_QUERY_LEN+1];
58 MsgDbHandler *dbHandle = getDbHandle();
60 memset(sqlQuery, 0x00, sizeof(sqlQuery));
61 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET FOLDER_NAME = '%s', FOLDER_TYPE = %d WHERE FOLDER_ID = %d;",
62 MSGFW_FOLDER_TABLE_NAME, pFolderInfo->folderName, pFolderInfo->folderType, pFolderInfo->folderId);
64 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS)
65 return MSG_ERR_DB_EXEC;
71 msg_error_t MsgStoDeleteFolder(const msg_folder_id_t folderId)
73 char sqlQuery[MAX_QUERY_LEN+1];
74 MsgDbHandler *dbHandle = getDbHandle();
75 dbHandle->beginTrans();
77 memset(sqlQuery, 0x00, sizeof(sqlQuery));
78 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE FOLDER_ID = %d;",
79 MSGFW_MESSAGE_TABLE_NAME, folderId);
81 // Delete Message in the folder from msg table
82 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
83 dbHandle->endTrans(false);
84 return MSG_ERR_DB_EXEC;
87 memset(sqlQuery, 0x00, sizeof(sqlQuery));
88 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE FOLDER_ID = %d;",
89 MSGFW_FOLDER_TABLE_NAME, folderId);
91 // Delete Message in the folder from msg table
92 if (dbHandle->execQuery(sqlQuery) != MSG_SUCCESS) {
93 dbHandle->endTrans(false);
94 return MSG_ERR_DB_EXEC;
97 // Clear Conversation table
98 if (MsgStoClearConversationTable(dbHandle) != MSG_SUCCESS) {
99 dbHandle->endTrans(false);
100 return MSG_ERR_DB_EXEC;
103 dbHandle->endTrans(true);
109 msg_error_t MsgStoGetFolderList(msg_struct_list_s *pFolderList)
111 if (pFolderList == NULL) {
112 MSG_DEBUG("pFolderList is NULL");
113 return MSG_ERR_NULL_POINTER;
115 MsgDbHandler *dbHandle = getDbHandle();
117 int rowCnt = 0, index = 0;
119 char sqlQuery[MAX_QUERY_LEN+1];
121 memset(sqlQuery, 0x00, sizeof(sqlQuery));
122 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT FOLDER_ID, FOLDER_TYPE, FOLDER_NAME FROM %s;",
123 MSGFW_FOLDER_TABLE_NAME);
125 if (dbHandle->getTable(sqlQuery, &rowCnt, &index) != MSG_SUCCESS) {
126 dbHandle->freeTable();
127 return MSG_ERR_DB_GETTABLE;
130 pFolderList->nCount = rowCnt;
132 MSG_DEBUG("pFolderList->nCount [%d]", pFolderList->nCount);
134 pFolderList->msg_struct_info = (msg_struct_t *)calloc(rowCnt, sizeof(MSG_FOLDER_INFO_S *));
136 msg_struct_s* pTmp = NULL;
138 for (int i = 0; i < rowCnt; i++) {
139 pFolderList->msg_struct_info[i] = (msg_struct_t)new msg_struct_s;
140 pTmp = (msg_struct_s *)pFolderList->msg_struct_info[i];
141 pTmp->type = MSG_STRUCT_FOLDER_INFO;
142 pTmp->data = new MSG_FOLDER_INFO_S;
143 MSG_FOLDER_INFO_S * pFolder = (MSG_FOLDER_INFO_S *)pTmp->data;
144 memset(pFolder, 0x00, sizeof(MSG_FOLDER_INFO_S));
146 pFolder->folderId = dbHandle->getColumnToInt(index++);
148 pFolder->folderType = dbHandle->getColumnToInt(index++);
150 memset(pFolder->folderName, 0x00, sizeof(pFolder->folderName));
151 dbHandle->getColumnToString(index++, MAX_FOLDER_NAME_SIZE, pFolder->folderName);
154 dbHandle->freeTable();