• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::MSG_CODE).append(" INTEGER DEFAULT 0)");
91 }
92 
CreateMmsProtocolTableStr(std::string & createTableStr)93 void RdbSmsMmsHelper::CreateMmsProtocolTableStr(std::string &createTableStr)
94 {
95     createTableStr.append("CREATE TABLE IF NOT EXISTS ").append(TABLE_MMS_PROTOCOL);
96     createTableStr.append("(").append(MmsProtocol::ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
97     createTableStr.append(SmsMmsInfo::MSG_ID).append(" INTEGER NOT NULL, ");
98     createTableStr.append(MmsProtocol::BCC).append(" TEXT DEFAULT '', ");
99     createTableStr.append(MmsProtocol::CC).append(" TEXT DEFAULT '', ");
100     createTableStr.append(MmsProtocol::CONTENT_LOCATION).append(" TEXT DEFAULT '', ");
101     createTableStr.append(MmsProtocol::DATE).append(" TEXT DEFAULT '', ");
102     createTableStr.append(MmsProtocol::DELIVERY_REPORT).append(" INTEGER DEFAULT 0, ");
103     createTableStr.append(MmsProtocol::DELIVERY_TIME).append(" TEXT DEFAULT '', ");
104     createTableStr.append(MmsProtocol::EXPIRY).append(" INTEGER , ");
105     createTableStr.append(MmsProtocol::TYPE).append(" INTEGER DEFAULT 0, ");
106     createTableStr.append(MmsProtocol::SERIAL_NUMBER).append(" TEXT DEFAULT '', ");
107     createTableStr.append(MmsProtocol::CATEGORY).append(" TEXT DEFAULT '', ");
108     createTableStr.append(MmsProtocol::VERSION).append(" INTEGER , ");
109     createTableStr.append(MmsProtocol::SIZE).append(" INTEGER , ");
110     createTableStr.append(MmsProtocol::PRIORITY).append(" INTEGER , ");
111     createTableStr.append(MmsProtocol::READ_REPLY).append(" INTEGER , ");
112     createTableStr.append(MmsProtocol::REPORT_ALLOWED).append(" INTEGER , ");
113     createTableStr.append(MmsProtocol::RESPONSE_STATUS).append(" INTEGER DEFAULT 0, ");
114     createTableStr.append(MmsProtocol::RESPONSE_TEXT).append(" TEXT DEFAULT '', ");
115     createTableStr.append(MmsProtocol::SENDER_VISIBILITY).append(" INTEGER DEFAULT 0,");
116     createTableStr.append("foreign key(").append(SmsMmsInfo::MSG_ID).append(") references ");
117     createTableStr.append(TABLE_SMS_MMS_INFO).append("(").append(SmsMmsInfo::MSG_ID);
118     createTableStr.append(") on delete cascade on update cascade )");
119 }
120 
CreateSmsSubsectionTableStr(std::string & createTableStr)121 void RdbSmsMmsHelper::CreateSmsSubsectionTableStr(std::string &createTableStr)
122 {
123     createTableStr.append("CREATE TABLE IF NOT EXISTS ").append(TABLE_SMS_SUBSECTION);
124     createTableStr.append("(").append(SmsSubsection::ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
125     createTableStr.append(SmsMmsInfo::SLOT_ID).append(" INTEGER DEFAULT 0, ");
126     createTableStr.append(SmsSubsection::SMS_SUBSECTION_ID).append(" INTEGER , ");
127     createTableStr.append(SmsSubsection::RECEIVER_NUMBER).append(" TEXT NOT NULL, ");
128     createTableStr.append(SmsSubsection::SENDER_NUMBER).append(" TEXT NOT NULL, ");
129     createTableStr.append(SmsSubsection::IS_SENDER).append(" INTEGER DEFAULT 0, ");
130     createTableStr.append(SmsSubsection::START_TIME).append(" TEXT DEFAULT '', ");
131     createTableStr.append(SmsSubsection::END_TIME).append(" TEXT DEFAULT '', ");
132     createTableStr.append(SmsSubsection::REW_PUD).append(" TEXT NOT NULL, ");
133     createTableStr.append(SmsSubsection::FORMAT).append(" INTEGER , ");
134     createTableStr.append(SmsSubsection::DEST_PORT).append(" INTEGER , ");
135     createTableStr.append(SmsSubsection::SUBSECTION_INDEX).append(" INTEGER , ");
136     createTableStr.append(SmsSubsection::SIZE).append(" INTEGER )");
137 }
138 
CreateMmsPartTableStr(std::string & createTableStr)139 void RdbSmsMmsHelper::CreateMmsPartTableStr(std::string &createTableStr)
140 {
141     createTableStr.append("CREATE TABLE IF NOT EXISTS ").append(TABLE_MMS_PART);
142     createTableStr.append("(").append(MmsPart::ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
143     createTableStr.append(SmsMmsInfo::MSG_ID).append(" INTEGER NOT NULL, ");
144     createTableStr.append(SmsMmsInfo::GROUP_ID).append(" INTEGER , ");
145     createTableStr.append(MmsPart::PART_INDEX).append(" INTEGER , ");
146     createTableStr.append(MmsPart::PART_SIZE).append(" TEXT , ");
147     createTableStr.append(MmsPart::RECORDING_TIME).append(" TEXT , ");
148     createTableStr.append(MmsPart::TYPE).append(" INTEGER , ");
149     createTableStr.append(MmsPart::LOCATION_PATH).append(" TEXT DEFAULT '', ");
150     createTableStr.append(MmsPart::ENCODE).append(" INTEGER , ");
151     createTableStr.append(MmsPart::STATE).append(" INTEGER , ");
152     createTableStr.append(MmsPart::CONTENT).append(" TEXT , ");
153     createTableStr.append("foreign key(").append(SmsMmsInfo::MSG_ID).append(") references ");
154     createTableStr.append(TABLE_SMS_MMS_INFO).append("(").append(SmsMmsInfo::MSG_ID);
155     createTableStr.append(") on delete cascade on update cascade )");
156 }
157 
CreateSessionTableStr(std::string & createTableStr)158 void RdbSmsMmsHelper::CreateSessionTableStr(std::string &createTableStr)
159 {
160     createTableStr.append("CREATE TABLE IF NOT EXISTS ").append(TABLE_SESSION);
161     createTableStr.append("(").append(Session::ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
162     createTableStr.append(Session::TIME).append(" INTEGER DEFAULT 0, ");
163     createTableStr.append(Session::TELEPHONE).append(" TEXT , ");
164     createTableStr.append(Session::CONTENT).append(" TEXT , ");
165     createTableStr.append(Session::CONTACTS_NUM).append(" INTEGER DEFAULT 0 , ");
166     createTableStr.append(Session::SMS_TYPE).append(" INTEGER DEFAULT 0 , ");
167     createTableStr.append(Session::UNREAD_COUNT).append(" INTEGER DEFAULT 0 , ");
168     createTableStr.append(Session::SENDING_STATUS).append(" INTEGER DEFAULT 0 , ");
169     createTableStr.append(Session::HAS_DRAFT).append(" INTEGER DEFAULT 0 , ");
170     createTableStr.append(Session::HAS_LOCK).append(" INTEGER DEFAULT 0 , ");
171     createTableStr.append(Session::MESSAGE_COUNT).append(" INTEGER DEFAULT 0 , ");
172     createTableStr.append(Session::HAS_MMS).append(" INTEGER DEFAULT 0 , ");
173     createTableStr.append(Session::HAS_ATTACHMENT).append(" INTEGER DEFAULT 0 )");
174 }
175 
CreateMmsPduTableStr(std::string & createTableStr)176 void RdbSmsMmsHelper::CreateMmsPduTableStr(std::string &createTableStr)
177 {
178     createTableStr.append("CREATE TABLE IF NOT EXISTS ").append(TABLE_MMS_PDU);
179     createTableStr.append("(").append(MmsPdu::ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
180     createTableStr.append(MmsPdu::PDU_CONTENT).append(" TEXT )");
181 }
182 
UpdateDbPath(const std::string & path)183 void RdbSmsMmsHelper::UpdateDbPath(const std::string &path)
184 {
185     dbPath_ = path + DB_NAME;
186 }
187 
DeleteDataByThirty()188 int32_t RdbSmsMmsHelper::DeleteDataByThirty()
189 {
190     int changedRows = 0;
191     int32_t result;
192     std::string values;
193     std::string date;
194     GetTimeOfThirty(date);
195     values.append(SmsMmsInfo::START_TIME).append("< '").append(date).append("'");
196     result = Delete(changedRows, TABLE_SMS_MMS_INFO, values);
197     return result;
198 }
199 
InsertSmsMmsInfo(int64_t & id,const NativeRdb::ValuesBucket & value)200 int RdbSmsMmsHelper::InsertSmsMmsInfo(int64_t &id, const NativeRdb::ValuesBucket &value)
201 {
202     return Insert(id, value, TABLE_SMS_MMS_INFO);
203 }
204 
BatchInsertSmsMmsInfo(int64_t & id,const std::vector<DataShare::DataShareValuesBucket> & values)205 int32_t RdbSmsMmsHelper::BatchInsertSmsMmsInfo(int64_t &id,
206     const std::vector<DataShare::DataShareValuesBucket> &values)
207 {
208     int32_t result = BeginTransaction();
209     if (result != NativeRdb::E_OK) {
210         DATA_STORAGE_LOGE("RdbSmsMmsHelper::BatchInsertSmsMmsInfo BeginTransaction error!");
211         return result;
212     }
213     for (const auto &item : values) {
214         OHOS::NativeRdb::ValuesBucket value = RdbDataShareAdapter::RdbUtils::ToValuesBucket(item);
215         result = InsertSmsMmsInfo(id, value);
216         if (result != NativeRdb::E_OK) {
217             DATA_STORAGE_LOGE("RdbSmsMmsHelper::InsertSmsMmsInfo error result = %{public}d", result);
218             RollBack();
219             return result;
220         }
221     }
222     result = CommitTransactionAction();
223     return result;
224 }
225 
CommitTransactionAction()226 int RdbSmsMmsHelper::CommitTransactionAction()
227 {
228     int result = Commit();
229     if (result != NativeRdb::E_OK) {
230         RollBack();
231     }
232     return result;
233 }
234 
QueryMaxGroupId()235 std::shared_ptr<NativeRdb::ResultSet> RdbSmsMmsHelper::QueryMaxGroupId()
236 {
237     std::string sql;
238     std::string maxGroupId("maxGroupId");
239     sql.append("select MAX(").append(SmsMmsInfo::GROUP_ID).append(") as ");
240     sql.append(maxGroupId).append(" from ") .append(TABLE_SMS_MMS_INFO);
241     return QuerySql(sql);
242 }
243 
StatisticsUnRead()244 std::shared_ptr<NativeRdb::ResultSet> RdbSmsMmsHelper::StatisticsUnRead()
245 {
246     std::string sql;
247     sql.append("select count(*) as totalListCount, ");
248     sql.append("count(CASE WHEN ").append(SmsMmsInfo::MSG_TYPE).append("=0 THEN 1 ELSE null END) as unreadCount, ");
249     sql.append("count(CASE WHEN ").append(SmsMmsInfo::MSG_TYPE).append("=1 THEN 1 ELSE null END) as unreadTotalOfInfo");
250     sql.append(" from ").append(TABLE_SMS_MMS_INFO).append(" WHERE ").append(SmsMmsInfo::IS_READ).append("=0");
251     return QuerySql(sql);
252 }
253 } // namespace Telephony
254 } // namespace OHOS
255