remove unnecessary dependancy to reduce memory use
[platform/core/messaging/msg-service.git] / include / utils / MsgSqliteWrapper.h
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 #ifndef MSG_SQLITE_WRAPPER_H
18 #define MSG_SQLITE_WRAPPER_H
19
20 /*==================================================================================================
21                                     INCLUDE FILES
22 ==================================================================================================*/
23 #include "MsgTypes.h"
24 #include "MsgMutex.h"
25 #include <sqlite3.h>
26
27 /*==================================================================================================
28                                     DEFINES
29 ==================================================================================================*/
30 #define MSGFW_DB_NAME                   TZ_SYS_DB_PATH"/.msg_service.db"
31
32 #define MSGFW_MESSAGE_TABLE_NAME                                "MSG_MESSAGE_TABLE"
33 #define MSGFW_FOLDER_TABLE_NAME                                 "MSG_FOLDER_TABLE"
34 #define MSGFW_ADDRESS_TABLE_NAME                        "MSG_ADDRESS_TABLE"
35 #define MSGFW_CONVERSATION_TABLE_NAME           "MSG_CONVERSATION_TABLE"
36 #define MSGFW_SIM_MSG_TABLE_NAME                                "MSG_SIM_TABLE"
37 #define MSGFW_FILTER_TABLE_NAME                                 "MSG_FILTER_TABLE"
38 #define MSGFW_PUSH_MSG_TABLE_NAME                       "MSG_PUSH_TABLE"
39 #define MSGFW_CB_MSG_TABLE_NAME                         "MSG_CBMSG_TABLE"
40 #define MMS_PLUGIN_MESSAGE_TABLE_NAME           "MSG_MMS_MESSAGE_TABLE"
41 #define MSGFW_SYNCML_MSG_TABLE_NAME             "MSG_SYNCML_TABLE"
42
43 #if 0
44 #define MSGFW_RECEIVED_CB_MSG_TABLE_NAME        "MSG_CB_MESSAGE_TABLE"
45 #endif
46
47 #define MSGFW_CB_CHANNEL_INFO_TABLE_NAME        "MSG_CB_CHANNEL_INFO_TABLE"
48 #ifdef FEATURE_SMS_CDMA
49 #define MSGFW_CDMA_CB_CHANNEL_INFO_TABLE_NAME           "MSG_CDMA_CB_CHANNEL_INFO_TABLE"
50 #endif
51
52 #define MSGFW_SMS_SENDOPT_TABLE_NAME            "MSG_SMS_SENDOPT_TABLE"
53 #define MSGFW_SMS_REPORT_TABLE_NAME             "MSG_SMS_REPORT_TABLE"
54 #define MSGFW_REPORT_TABLE_NAME                                 "MSG_REPORT_TABLE"
55 #define MSGFW_PUSH_CONFIG_TABLE_NAME            "MSG_PUSHCFG_TABLE"
56 #define MSGFW_MMS_PREVIEW_TABLE_NAME            "MSG_MMS_PREVIEW_INFO_TABLE"
57 #define MSGFW_MMS_MULTIPART_TABLE_NAME          "MSG_MULTIPART_TABLE"
58
59 #ifdef FEATURE_SMS_CDMA
60 #define MSGFW_UNIQUENESS_INFO_TABLE_NAME        "MSG_UNIQUENESS_INFO_TABLE"
61 #endif
62
63 #define MSGFW_TMP_MSGID_TABLE_NAME                      "MSG_TMP_MSGID_TABLE"
64
65 #define MSGFW_ADDRESS_TEMP_TABLE_NAME           "MSG_ADDRESS_TEMP_TABLE"
66
67 #define MAX_QUERY_LEN                                   3072
68 #define MAX_FOLDER_NAME_LEN             20
69 #define MAX_ACCOUNT_NAME_LEN    51
70
71 #define MSGFW_DB_ESCAPE_CHAR            '\\'
72
73 #define SHM_FILE_FOR_DB_LOCK                "/.msg_shm_db_lock"
74
75 /*==================================================================================================
76                                      FUNCTION PROTOTYPES
77 ==================================================================================================*/
78 void    MsgReleaseMemoryDB();
79 msg_error_t MsgConvertStrWithEscape(const char *input, char **output);
80
81
82 /*==================================================================================================
83                                      CLASS DEFINITIONS
84 ==================================================================================================*/
85 class MsgDbHandler
86 {
87 public:
88         MsgDbHandler();
89         ~MsgDbHandler();
90
91         msg_error_t connect();
92         msg_error_t connectReadOnly();
93         msg_error_t disconnect();
94
95         bool checkTableExist(const char *pTableName);
96         msg_error_t execQuery(const char *pQuery);
97         msg_error_t getTable(const char *pQuery, int *pRowCnt, int *pColumnCnt);
98         msg_error_t getTableWithResult(const char *pQuery, char ***res, int *pRowCnt, int *pColumnCnt);
99         void freeTable();
100         void freeTable(char **db_res);
101         msg_error_t bindText(const char *pBindStr, int index);
102         msg_error_t bindInt(const int pBindint, int index);
103         msg_error_t bindBlob(const void * pBindBlob, int size, int index);
104         msg_error_t prepareQuery(const char *pQuery);
105         msg_error_t stepQuery();
106         void resetQuery();
107         void finalizeQuery();
108         int columnInt(int ColumnIndex);
109         const unsigned char* columnText(int ColumnIndex);
110         const void* columnBlob(int ColumnIndex);
111         msg_error_t beginTrans();
112         msg_error_t endTrans(bool Success);
113         int getColumnToInt(int RowIndex);
114         char getColumnToChar(int RowIndex);
115         char* getColumnToString(int RowIndex);
116         void getColumnToString(int RowIndex, int Length, char *pString);
117         msg_error_t getRowId(const char *pTableName, unsigned int *pRowId);
118         void getMmapMutex(const char *shm_file_name);
119         void shm_mutex_timedlock(int sec);
120         void shm_mutex_unlock();
121
122 private:
123         char **result;
124         sqlite3 *handle;
125         sqlite3_stmt *stmt;
126         pthread_mutex_t *mmapMx;
127         int shm_fd;
128         bool in_trans;
129 };
130
131
132 MsgDbHandler *getDbHandle();
133 void removeDbHandle();
134 #endif /* MSG_SQLITE_WRAPPER_H */
135