1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "rdb_sms_mms_helper.h"
17
18 #include "rdb_errno.h"
19 #include "rdb_sms_mms_callback.h"
20 #include "rdb_store_config.h"
21 #include "rdb_utils.h"
22 #include "sms_mms_data.h"
23 #include "time_util.h"
24
25 namespace OHOS {
26 namespace NativeRdb {
27 class ResultSet;
28 class ValuesBucket;
29 } // namespace NativeRdb
30 namespace Telephony {
RdbSmsMmsHelper()31 RdbSmsMmsHelper::RdbSmsMmsHelper()
32 {
33 }
34
Init()35 int RdbSmsMmsHelper::Init()
36 {
37 int errCode = NativeRdb::E_OK;
38 NativeRdb::RdbStoreConfig config(dbPath_);
39 config.SetJournalMode(NativeRdb::JournalMode::MODE_TRUNCATE);
40 std::string messageInfoStr;
41 CreateSmsMmsInfoTableStr(messageInfoStr);
42 std::string mmsProtocolStr;
43 CreateMmsProtocolTableStr(mmsProtocolStr);
44 std::string smsSubsectionStr;
45 CreateSmsSubsectionTableStr(smsSubsectionStr);
46 std::string mmsPartStr;
47 CreateMmsPartTableStr(mmsPartStr);
48 std::string sessionStr;
49 CreateSessionTableStr(sessionStr);
50 std::string mmsPduStr;
51 CreateMmsPduTableStr(mmsPduStr);
52 std::vector<std::string> createTableVec;
53 createTableVec.push_back(messageInfoStr);
54 createTableVec.push_back(mmsProtocolStr);
55 createTableVec.push_back(smsSubsectionStr);
56 createTableVec.push_back(mmsPartStr);
57 createTableVec.push_back(sessionStr);
58 createTableVec.push_back(mmsPduStr);
59 RdbSmsMmsCallback callback(createTableVec);
60 CreateRdbStore(config, VERSION, callback, errCode);
61 return errCode;
62 }
63
CreateSmsMmsInfoTableStr(std::string & createTableStr)64 void RdbSmsMmsHelper::CreateSmsMmsInfoTableStr(std::string &createTableStr)
65 {
66 createTableStr.append("CREATE TABLE IF NOT EXISTS ").append(TABLE_SMS_MMS_INFO);
67 createTableStr.append("(").append(SmsMmsInfo::MSG_ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
68 createTableStr.append(SmsMmsInfo::SLOT_ID).append(" INTEGER DEFAULT 0, ");
69 createTableStr.append(SmsMmsInfo::RECEIVER_NUMBER).append(" TEXT , ");
70 createTableStr.append(SmsMmsInfo::SENDER_NUMBER).append(" TEXT , ");
71 createTableStr.append(SmsMmsInfo::IS_SENDER).append(" INTEGER DEFAULT 0, ");
72 createTableStr.append(SmsMmsInfo::MSG_TYPE).append(" INTEGER DEFAULT 0, ");
73 createTableStr.append(SmsMmsInfo::SMS_TYPE).append(" INTEGER DEFAULT 0, ");
74 createTableStr.append(SmsMmsInfo::START_TIME).append(" TEXT DEFAULT '', ");
75 createTableStr.append(SmsMmsInfo::END_TIME).append(" TEXT DEFAULT '', ");
76 createTableStr.append(SmsMmsInfo::MSG_STATE).append(" INTEGER DEFAULT 0, ");
77 createTableStr.append(SmsMmsInfo::MSG_TITLE).append(" TEXT DEFAULT '', ");
78 createTableStr.append(SmsMmsInfo::MSG_CONTENT).append(" TEXT DEFAULT '', ");
79 createTableStr.append(SmsMmsInfo::OPERATOR_SERVICE_NUMBER).append(" TEXT DEFAULT '', ");
80 createTableStr.append(SmsMmsInfo::IS_LOCK).append(" INTEGER DEFAULT 0, ");
81 createTableStr.append(SmsMmsInfo::IS_COLLECT).append(" INTEGER DEFAULT 0, ");
82 createTableStr.append(SmsMmsInfo::IS_READ).append(" INTEGER DEFAULT 0, ");
83 createTableStr.append(SmsMmsInfo::SESSION_TYPE).append(" INTEGER DEFAULT 3, ");
84 createTableStr.append(SmsMmsInfo::RETRY_NUMBER).append(" INTEGER DEFAULT 0, ");
85 createTableStr.append(SmsMmsInfo::SESSION_ID).append(" INTEGER DEFAULT -1, ");
86 createTableStr.append(SmsMmsInfo::GROUP_ID).append(" INTEGER DEFAULT -1, ");
87 createTableStr.append(SmsMmsInfo::DEVICE_ID).append(" INTEGER , ");
88 createTableStr.append(SmsMmsInfo::IS_SUBSECTION).append(" INTEGER DEFAULT 0, ");
89 createTableStr.append(SmsMmsInfo::IS_SEND_REPORT).append(" INTEGER DEFAULT 0, ");
90 createTableStr.append(SmsMmsInfo::IS_ADVANCED_SECURITY).append(" INTEGER DEFAULT 0, ");
91 createTableStr.append(SmsMmsInfo::MSG_CODE).append(" INTEGER DEFAULT 0)");
92 }
93
CreateMmsProtocolTableStr(std::string & createTableStr)94 void RdbSmsMmsHelper::CreateMmsProtocolTableStr(std::string &createTableStr)
95 {
96 createTableStr.append("CREATE TABLE IF NOT EXISTS ").append(TABLE_MMS_PROTOCOL);
97 createTableStr.append("(").append(MmsProtocol::ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
98 createTableStr.append(SmsMmsInfo::MSG_ID).append(" INTEGER NOT NULL, ");
99 createTableStr.append(MmsProtocol::BCC).append(" TEXT DEFAULT '', ");
100 createTableStr.append(MmsProtocol::CC).append(" TEXT DEFAULT '', ");
101 createTableStr.append(MmsProtocol::CONTENT_LOCATION).append(" TEXT DEFAULT '', ");
102 createTableStr.append(MmsProtocol::DATE).append(" TEXT DEFAULT '', ");
103 createTableStr.append(MmsProtocol::DELIVERY_REPORT).append(" INTEGER DEFAULT 0, ");
104 createTableStr.append(MmsProtocol::DELIVERY_TIME).append(" TEXT DEFAULT '', ");
105 createTableStr.append(MmsProtocol::EXPIRY).append(" INTEGER , ");
106 createTableStr.append(MmsProtocol::TYPE).append(" INTEGER DEFAULT 0, ");
107 createTableStr.append(MmsProtocol::SERIAL_NUMBER).append(" TEXT DEFAULT '', ");
108 createTableStr.append(MmsProtocol::CATEGORY).append(" TEXT DEFAULT '', ");
109 createTableStr.append(MmsProtocol::VERSION).append(" INTEGER , ");
110 createTableStr.append(MmsProtocol::SIZE).append(" INTEGER , ");
111 createTableStr.append(MmsProtocol::PRIORITY).append(" INTEGER , ");
112 createTableStr.append(MmsProtocol::READ_REPLY).append(" INTEGER , ");
113 createTableStr.append(MmsProtocol::REPORT_ALLOWED).append(" INTEGER , ");
114 createTableStr.append(MmsProtocol::RESPONSE_STATUS).append(" INTEGER DEFAULT 0, ");
115 createTableStr.append(MmsProtocol::RESPONSE_TEXT).append(" TEXT DEFAULT '', ");
116 createTableStr.append(MmsProtocol::SENDER_VISIBILITY).append(" INTEGER DEFAULT 0,");
117 createTableStr.append("foreign key(").append(SmsMmsInfo::MSG_ID).append(") references ");
118 createTableStr.append(TABLE_SMS_MMS_INFO).append("(").append(SmsMmsInfo::MSG_ID);
119 createTableStr.append(") on delete cascade on update cascade )");
120 }
121
CreateSmsSubsectionTableStr(std::string & createTableStr)122 void RdbSmsMmsHelper::CreateSmsSubsectionTableStr(std::string &createTableStr)
123 {
124 createTableStr.append("CREATE TABLE IF NOT EXISTS ").append(TABLE_SMS_SUBSECTION);
125 createTableStr.append("(").append(SmsSubsection::ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
126 createTableStr.append(SmsMmsInfo::SLOT_ID).append(" INTEGER DEFAULT 0, ");
127 createTableStr.append(SmsSubsection::SMS_SUBSECTION_ID).append(" INTEGER , ");
128 createTableStr.append(SmsSubsection::RECEIVER_NUMBER).append(" TEXT NOT NULL, ");
129 createTableStr.append(SmsSubsection::SENDER_NUMBER).append(" TEXT NOT NULL, ");
130 createTableStr.append(SmsSubsection::IS_SENDER).append(" INTEGER DEFAULT 0, ");
131 createTableStr.append(SmsSubsection::START_TIME).append(" TEXT DEFAULT '', ");
132 createTableStr.append(SmsSubsection::END_TIME).append(" TEXT DEFAULT '', ");
133 createTableStr.append(SmsSubsection::REW_PUD).append(" TEXT NOT NULL, ");
134 createTableStr.append(SmsSubsection::FORMAT).append(" INTEGER , ");
135 createTableStr.append(SmsSubsection::DEST_PORT).append(" INTEGER , ");
136 createTableStr.append(SmsSubsection::SUBSECTION_INDEX).append(" INTEGER , ");
137 createTableStr.append(SmsSubsection::SIZE).append(" INTEGER )");
138 }
139
CreateMmsPartTableStr(std::string & createTableStr)140 void RdbSmsMmsHelper::CreateMmsPartTableStr(std::string &createTableStr)
141 {
142 createTableStr.append("CREATE TABLE IF NOT EXISTS ").append(TABLE_MMS_PART);
143 createTableStr.append("(").append(MmsPart::ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
144 createTableStr.append(SmsMmsInfo::MSG_ID).append(" INTEGER NOT NULL, ");
145 createTableStr.append(SmsMmsInfo::GROUP_ID).append(" INTEGER , ");
146 createTableStr.append(MmsPart::PART_INDEX).append(" INTEGER , ");
147 createTableStr.append(MmsPart::PART_SIZE).append(" TEXT , ");
148 createTableStr.append(MmsPart::RECORDING_TIME).append(" TEXT , ");
149 createTableStr.append(MmsPart::TYPE).append(" INTEGER , ");
150 createTableStr.append(MmsPart::LOCATION_PATH).append(" TEXT DEFAULT '', ");
151 createTableStr.append(MmsPart::ENCODE).append(" INTEGER , ");
152 createTableStr.append(MmsPart::STATE).append(" INTEGER , ");
153 createTableStr.append(MmsPart::CONTENT).append(" TEXT , ");
154 createTableStr.append("foreign key(").append(SmsMmsInfo::MSG_ID).append(") references ");
155 createTableStr.append(TABLE_SMS_MMS_INFO).append("(").append(SmsMmsInfo::MSG_ID);
156 createTableStr.append(") on delete cascade on update cascade )");
157 }
158
CreateSessionTableStr(std::string & createTableStr)159 void RdbSmsMmsHelper::CreateSessionTableStr(std::string &createTableStr)
160 {
161 createTableStr.append("CREATE TABLE IF NOT EXISTS ").append(TABLE_SESSION);
162 createTableStr.append("(").append(Session::ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
163 createTableStr.append(Session::TIME).append(" INTEGER DEFAULT 0, ");
164 createTableStr.append(Session::TELEPHONE).append(" TEXT , ");
165 createTableStr.append(Session::CONTENT).append(" TEXT , ");
166 createTableStr.append(Session::CONTACTS_NUM).append(" INTEGER DEFAULT 0 , ");
167 createTableStr.append(Session::SMS_TYPE).append(" INTEGER DEFAULT 0 , ");
168 createTableStr.append(Session::UNREAD_COUNT).append(" INTEGER DEFAULT 0 , ");
169 createTableStr.append(Session::SENDING_STATUS).append(" INTEGER DEFAULT 0 , ");
170 createTableStr.append(Session::HAS_DRAFT).append(" INTEGER DEFAULT 0 , ");
171 createTableStr.append(Session::HAS_LOCK).append(" INTEGER DEFAULT 0 , ");
172 createTableStr.append(Session::MESSAGE_COUNT).append(" INTEGER DEFAULT 0 , ");
173 createTableStr.append(Session::HAS_MMS).append(" INTEGER DEFAULT 0 , ");
174 createTableStr.append(Session::HAS_ATTACHMENT).append(" INTEGER DEFAULT 0 )");
175 }
176
CreateMmsPduTableStr(std::string & createTableStr)177 void RdbSmsMmsHelper::CreateMmsPduTableStr(std::string &createTableStr)
178 {
179 createTableStr.append("CREATE TABLE IF NOT EXISTS ").append(TABLE_MMS_PDU);
180 createTableStr.append("(").append(MmsPdu::ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
181 createTableStr.append(MmsPdu::PDU_CONTENT).append(" TEXT )");
182 }
183
UpdateDbPath(const std::string & path)184 void RdbSmsMmsHelper::UpdateDbPath(const std::string &path)
185 {
186 dbPath_ = path + DB_NAME;
187 }
188
DeleteDataByThirty()189 int32_t RdbSmsMmsHelper::DeleteDataByThirty()
190 {
191 int changedRows = 0;
192 int32_t result;
193 std::string values;
194 std::string date;
195 GetTimeOfThirty(date);
196 values.append(SmsMmsInfo::START_TIME).append("< '").append(date).append("'");
197 result = Delete(changedRows, TABLE_SMS_MMS_INFO, values);
198 return result;
199 }
200
InsertSmsMmsInfo(int64_t & id,const NativeRdb::ValuesBucket & value)201 int RdbSmsMmsHelper::InsertSmsMmsInfo(int64_t &id, const NativeRdb::ValuesBucket &value)
202 {
203 return Insert(id, value, TABLE_SMS_MMS_INFO);
204 }
205
BatchInsertSmsMmsInfo(int64_t & id,const std::vector<DataShare::DataShareValuesBucket> & values)206 int32_t RdbSmsMmsHelper::BatchInsertSmsMmsInfo(int64_t &id,
207 const std::vector<DataShare::DataShareValuesBucket> &values)
208 {
209 int32_t result = BeginTransaction();
210 if (result != NativeRdb::E_OK) {
211 DATA_STORAGE_LOGE("RdbSmsMmsHelper::BatchInsertSmsMmsInfo BeginTransaction error!");
212 return result;
213 }
214 for (const auto &item : values) {
215 OHOS::NativeRdb::ValuesBucket value = RdbDataShareAdapter::RdbUtils::ToValuesBucket(item);
216 result = InsertSmsMmsInfo(id, value);
217 if (result != NativeRdb::E_OK) {
218 DATA_STORAGE_LOGE("RdbSmsMmsHelper::InsertSmsMmsInfo error result = %{public}d", result);
219 RollBack();
220 return result;
221 }
222 }
223 result = CommitTransactionAction();
224 return result;
225 }
226
CommitTransactionAction()227 int RdbSmsMmsHelper::CommitTransactionAction()
228 {
229 int result = Commit();
230 if (result != NativeRdb::E_OK) {
231 RollBack();
232 }
233 return result;
234 }
235
QueryMaxGroupId()236 std::shared_ptr<NativeRdb::ResultSet> RdbSmsMmsHelper::QueryMaxGroupId()
237 {
238 std::string sql;
239 std::string maxGroupId("maxGroupId");
240 sql.append("select MAX(").append(SmsMmsInfo::GROUP_ID).append(") as ");
241 sql.append(maxGroupId).append(" from ") .append(TABLE_SMS_MMS_INFO);
242 return QuerySql(sql);
243 }
244
StatisticsUnRead()245 std::shared_ptr<NativeRdb::ResultSet> RdbSmsMmsHelper::StatisticsUnRead()
246 {
247 std::string sql;
248 sql.append("select count(*) as totalListCount, ");
249 sql.append("count(CASE WHEN ").append(SmsMmsInfo::MSG_TYPE).append("=0 THEN 1 ELSE null END) as unreadCount, ");
250 sql.append("count(CASE WHEN ").append(SmsMmsInfo::MSG_TYPE).append("=1 THEN 1 ELSE null END) as unreadTotalOfInfo");
251 sql.append(" from ").append(TABLE_SMS_MMS_INFO).append(" WHERE ").append(SmsMmsInfo::IS_READ).append("=0");
252 return QuerySql(sql);
253 }
254 } // namespace Telephony
255 } // namespace OHOS
256