2 * Copyright 2012-2013 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (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://floralicense.org/license/
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 ==================================================================================================*/
26 extern MsgDbHandler dbHandle;
29 /*==================================================================================================
30 FUNCTION IMPLEMENTATION
31 ==================================================================================================*/
32 msg_error_t MsgStoAddFolder(const MSG_FOLDER_INFO_S *pFolderInfo)
34 msg_error_t err = MSG_SUCCESS;
36 unsigned int rowId = 0;
38 char sqlQuery[MAX_QUERY_LEN+1];
40 err = dbHandle.getRowId(MSGFW_FOLDER_TABLE_NAME, &rowId);
42 if (err != MSG_SUCCESS)
46 memset(sqlQuery, 0x00, sizeof(sqlQuery));
47 snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d, '%s', %d);",
48 MSGFW_FOLDER_TABLE_NAME, rowId, pFolderInfo->folderName, pFolderInfo->folderType);
50 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
51 return MSG_ERR_DB_EXEC;
57 msg_error_t MsgStoUpdateFolder(const MSG_FOLDER_INFO_S *pFolderInfo)
59 char sqlQuery[MAX_QUERY_LEN+1];
62 memset(sqlQuery, 0x00, sizeof(sqlQuery));
63 snprintf(sqlQuery, sizeof(sqlQuery), "UPDATE %s SET FOLDER_NAME = '%s', FOLDER_TYPE = %d WHERE FOLDER_ID = %d;",
64 MSGFW_FOLDER_TABLE_NAME, pFolderInfo->folderName, pFolderInfo->folderType, pFolderInfo->folderId);
66 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS)
67 return MSG_ERR_DB_EXEC;
73 msg_error_t MsgStoDeleteFolder(const msg_folder_id_t folderId)
75 char sqlQuery[MAX_QUERY_LEN+1];
77 dbHandle.beginTrans();
79 memset(sqlQuery, 0x00, sizeof(sqlQuery));
80 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE FOLDER_ID = %d;",
81 MSGFW_MESSAGE_TABLE_NAME, folderId);
83 // Delete Message in the folder from msg table
84 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
85 dbHandle.endTrans(false);
86 return MSG_ERR_DB_EXEC;
89 memset(sqlQuery, 0x00, sizeof(sqlQuery));
90 snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE FOLDER_ID = %d;",
91 MSGFW_FOLDER_TABLE_NAME, folderId);
93 // Delete Message in the folder from msg table
94 if (dbHandle.execQuery(sqlQuery) != MSG_SUCCESS) {
95 dbHandle.endTrans(false);
96 return MSG_ERR_DB_EXEC;
99 // Clear Conversation table
100 if (MsgStoClearConversationTable(&dbHandle) != MSG_SUCCESS) {
101 dbHandle.endTrans(false);
102 return MSG_ERR_DB_EXEC;
105 dbHandle.endTrans(true);
111 msg_error_t MsgStoGetFolderList(msg_struct_list_s *pFolderList)
113 if (pFolderList == NULL) {
114 MSG_DEBUG("pFolderList is NULL");
115 return MSG_ERR_NULL_POINTER;
121 char sqlQuery[MAX_QUERY_LEN+1];
123 memset(sqlQuery, 0x00, sizeof(sqlQuery));
124 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT FOLDER_ID, FOLDER_TYPE, FOLDER_NAME FROM %s;",
125 MSGFW_FOLDER_TABLE_NAME);
127 if (dbHandle.getTable(sqlQuery, &rowCnt) != MSG_SUCCESS) {
128 dbHandle.freeTable();
129 return MSG_ERR_DB_GETTABLE;
132 pFolderList->nCount = rowCnt;
134 MSG_DEBUG("pFolderList->nCount [%d]", pFolderList->nCount);
136 pFolderList->msg_struct_info = (msg_struct_t *)new char[sizeof(MSG_FOLDER_INFO_S *)*rowCnt];
138 msg_struct_s* pTmp = NULL;
140 for (int i = 0; i < rowCnt; i++)
142 pFolderList->msg_struct_info[i] = (msg_struct_t)new char[sizeof(msg_struct_s)];
143 pTmp = (msg_struct_s *)pFolderList->msg_struct_info[i];
144 pTmp->type = MSG_STRUCT_FOLDER_INFO;
145 pTmp->data = new char[sizeof(MSG_FOLDER_INFO_S)];
146 MSG_FOLDER_INFO_S * pFolder = (MSG_FOLDER_INFO_S *)pTmp->data;
147 memset(pFolder, 0x00, sizeof(MSG_FOLDER_INFO_S));
149 pFolder->folderId = dbHandle.getColumnToInt(index++);
151 pFolder->folderType = dbHandle.getColumnToInt(index++);
153 memset(pFolder->folderName, 0x00, sizeof(pFolder->folderName));
154 dbHandle.getColumnToString(index++, MAX_FOLDER_NAME_SIZE, pFolder->folderName);
158 dbHandle.freeTable();