• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 "sms_mms_data.h"
22 #include "time_util.h"
23 
24 namespace OHOS {
25 namespace NativeRdb {
26 class AbsSharedResultSet;
27 class ValuesBucket;
28 } // namespace NativeRdb
29 namespace Telephony {
RdbSmsMmsHelper()30 RdbSmsMmsHelper::RdbSmsMmsHelper()
31 {
32 }
33 
Init()34 int RdbSmsMmsHelper::Init()
35 {
36     int errCode = NativeRdb::E_OK;
37     NativeRdb::RdbStoreConfig config(dbPath_);
38     config.SetJournalMode(NativeRdb::JournalMode::MODE_TRUNCATE);
39     std::string messageInfoStr;
40     CreateSmsMmsInfoTableStr(messageInfoStr);
41     std::string mmsProtocolStr;
42     CreateMmsProtocolTableStr(mmsProtocolStr);
43     std::string smsSubsectionStr;
44     CreateSmsSubsectionTableStr(smsSubsectionStr);
45     std::string mmsPartStr;
46     CreateMmsPartTableStr(mmsPartStr);
47     std::vector<std::string> createTableVec;
48     createTableVec.push_back(messageInfoStr);
49     createTableVec.push_back(mmsProtocolStr);
50     createTableVec.push_back(smsSubsectionStr);
51     createTableVec.push_back(mmsPartStr);
52     RdbSmsMmsCallback callback(createTableVec);
53     CreateRdbStore(config, VERSION, callback, errCode);
54     return errCode;
55 }
56 
CreateSmsMmsInfoTableStr(std::string & createTableStr)57 void RdbSmsMmsHelper::CreateSmsMmsInfoTableStr(std::string &createTableStr)
58 {
59     createTableStr.append("CREATE TABLE IF NOT EXISTS ").append(TABLE_SMS_MMS_INFO);
60     createTableStr.append("(").append(SmsMmsInfo::MSG_ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
61     createTableStr.append(SmsMmsInfo::SLOT_ID).append(" INTEGER DEFAULT 0, ");
62     createTableStr.append(SmsMmsInfo::RECEIVER_NUMBER).append(" TEXT , ");
63     createTableStr.append(SmsMmsInfo::SENDER_NUMBER).append(" TEXT , ");
64     createTableStr.append(SmsMmsInfo::IS_SENDER).append(" INTEGER DEFAULT 0, ");
65     createTableStr.append(SmsMmsInfo::MSG_TYPE).append(" INTEGER DEFAULT 0, ");
66     createTableStr.append(SmsMmsInfo::SMS_TYPE).append(" INTEGER DEFAULT 0, ");
67     createTableStr.append(SmsMmsInfo::START_TIME).append(" TEXT DEFAULT '', ");
68     createTableStr.append(SmsMmsInfo::END_TIME).append(" TEXT DEFAULT '', ");
69     createTableStr.append(SmsMmsInfo::MSG_STATE).append(" INTEGER DEFAULT 0, ");
70     createTableStr.append(SmsMmsInfo::MSG_TITLE).append(" TEXT DEFAULT '', ");
71     createTableStr.append(SmsMmsInfo::MSG_CONTENT).append(" TEXT DEFAULT '', ");
72     createTableStr.append(SmsMmsInfo::OPERATOR_SERVICE_NUMBER).append(" TEXT DEFAULT '', ");
73     createTableStr.append(SmsMmsInfo::IS_LOCK).append(" INTEGER DEFAULT 0, ");
74     createTableStr.append(SmsMmsInfo::IS_COLLECT).append(" INTEGER DEFAULT 0, ");
75     createTableStr.append(SmsMmsInfo::IS_READ).append(" INTEGER DEFAULT 0, ");
76     createTableStr.append(SmsMmsInfo::SESSION_TYPE).append(" INTEGER DEFAULT 3, ");
77     createTableStr.append(SmsMmsInfo::RETRY_NUMBER).append(" INTEGER DEFAULT 0, ");
78     createTableStr.append(SmsMmsInfo::SESSION_ID).append(" INTEGER DEFAULT -1, ");
79     createTableStr.append(SmsMmsInfo::GROUP_ID).append(" INTEGER DEFAULT -1, ");
80     createTableStr.append(SmsMmsInfo::DEVICE_ID).append(" INTEGER , ");
81     createTableStr.append(SmsMmsInfo::IS_SUBSECTION).append(" INTEGER DEFAULT 0, ");
82     createTableStr.append(SmsMmsInfo::IS_SEND_REPORT).append(" INTEGER DEFAULT 0, ");
83     createTableStr.append(SmsMmsInfo::MSG_CODE).append(" INTEGER DEFAULT 0)");
84 }
85 
CreateMmsProtocolTableStr(std::string & createTableStr)86 void RdbSmsMmsHelper::CreateMmsProtocolTableStr(std::string &createTableStr)
87 {
88     createTableStr.append("CREATE TABLE IF NOT EXISTS ").append(TABLE_MMS_PROTOCOL);
89     createTableStr.append("(").append(MmsProtocol::ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
90     createTableStr.append(SmsMmsInfo::MSG_ID).append(" INTEGER NOT NULL, ");
91     createTableStr.append(MmsProtocol::BCC).append(" TEXT DEFAULT '', ");
92     createTableStr.append(MmsProtocol::CC).append(" TEXT DEFAULT '', ");
93     createTableStr.append(MmsProtocol::CONTENT_LOCATION).append(" TEXT DEFAULT '', ");
94     createTableStr.append(MmsProtocol::DATE).append(" TEXT DEFAULT '', ");
95     createTableStr.append(MmsProtocol::DELIVERY_REPORT).append(" INTEGER DEFAULT 0, ");
96     createTableStr.append(MmsProtocol::DELIVERY_TIME).append(" TEXT DEFAULT '', ");
97     createTableStr.append(MmsProtocol::EXPIRY).append(" INTEGER , ");
98     createTableStr.append(MmsProtocol::TYPE).append(" INTEGER DEFAULT 0, ");
99     createTableStr.append(MmsProtocol::SERIAL_NUMBER).append(" TEXT DEFAULT '', ");
100     createTableStr.append(MmsProtocol::CATEGORY).append(" TEXT DEFAULT '', ");
101     createTableStr.append(MmsProtocol::VERSION).append(" INTEGER , ");
102     createTableStr.append(MmsProtocol::SIZE).append(" INTEGER , ");
103     createTableStr.append(MmsProtocol::PRIORITY).append(" INTEGER , ");
104     createTableStr.append(MmsProtocol::READ_REPLY).append(" INTEGER , ");
105     createTableStr.append(MmsProtocol::REPORT_ALLOWED).append(" INTEGER , ");
106     createTableStr.append(MmsProtocol::RESPONSE_STATUS).append(" INTEGER DEFAULT 0, ");
107     createTableStr.append(MmsProtocol::RESPONSE_TEXT).append(" TEXT DEFAULT '', ");
108     createTableStr.append(MmsProtocol::SENDER_VISIBILITY).append(" INTEGER DEFAULT 0,");
109     createTableStr.append("foreign key(").append(SmsMmsInfo::MSG_ID).append(") references ");
110     createTableStr.append(TABLE_SMS_MMS_INFO).append("(").append(SmsMmsInfo::MSG_ID);
111     createTableStr.append(") on delete cascade on update cascade )");
112 }
113 
CreateSmsSubsectionTableStr(std::string & createTableStr)114 void RdbSmsMmsHelper::CreateSmsSubsectionTableStr(std::string &createTableStr)
115 {
116     createTableStr.append("CREATE TABLE IF NOT EXISTS ").append(TABLE_SMS_SUBSECTION);
117     createTableStr.append("(").append(SmsSubsection::ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
118     createTableStr.append(SmsMmsInfo::SLOT_ID).append(" INTEGER DEFAULT 0, ");
119     createTableStr.append(SmsSubsection::SMS_SUBSECTION_ID).append(" INTEGER , ");
120     createTableStr.append(SmsSubsection::RECEIVER_NUMBER).append(" TEXT NOT NULL, ");
121     createTableStr.append(SmsSubsection::SENDER_NUMBER).append(" TEXT NOT NULL, ");
122     createTableStr.append(SmsSubsection::IS_SENDER).append(" INTEGER DEFAULT 0, ");
123     createTableStr.append(SmsSubsection::START_TIME).append(" TEXT DEFAULT '', ");
124     createTableStr.append(SmsSubsection::END_TIME).append(" TEXT DEFAULT '', ");
125     createTableStr.append(SmsSubsection::REW_PUD).append(" TEXT NOT NULL, ");
126     createTableStr.append(SmsSubsection::FORMAT).append(" INTEGER , ");
127     createTableStr.append(SmsSubsection::DEST_PORT).append(" INTEGER , ");
128     createTableStr.append(SmsSubsection::SUBSECTION_INDEX).append(" INTEGER , ");
129     createTableStr.append(SmsSubsection::SIZE).append(" INTEGER )");
130 }
131 
CreateMmsPartTableStr(std::string & createTableStr)132 void RdbSmsMmsHelper::CreateMmsPartTableStr(std::string &createTableStr)
133 {
134     createTableStr.append("CREATE TABLE IF NOT EXISTS ").append(TABLE_MMS_PART);
135     createTableStr.append("(").append(MmsPart::ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
136     createTableStr.append(SmsMmsInfo::MSG_ID).append(" INTEGER NOT NULL, ");
137     createTableStr.append(SmsMmsInfo::GROUP_ID).append(" INTEGER , ");
138     createTableStr.append(MmsPart::PART_INDEX).append(" INTEGER , ");
139     createTableStr.append(MmsPart::PART_SIZE).append(" TEXT , ");
140     createTableStr.append(MmsPart::RECORDING_TIME).append(" TEXT , ");
141     createTableStr.append(MmsPart::TYPE).append(" INTEGER , ");
142     createTableStr.append(MmsPart::LOCATION_PATH).append(" TEXT DEFAULT '', ");
143     createTableStr.append(MmsPart::ENCODE).append(" INTEGER , ");
144     createTableStr.append(MmsPart::STATE).append(" INTEGER , ");
145     createTableStr.append(MmsPart::CONTENT).append(" TEXT , ");
146     createTableStr.append("foreign key(").append(SmsMmsInfo::MSG_ID).append(") references ");
147     createTableStr.append(TABLE_SMS_MMS_INFO).append("(").append(SmsMmsInfo::MSG_ID);
148     createTableStr.append(") on delete cascade on update cascade )");
149 }
150 
UpdateDbPath(const std::string & path)151 void RdbSmsMmsHelper::UpdateDbPath(const std::string &path)
152 {
153     dbPath_ = path + DB_NAME;
154 }
155 
DeleteDataByThirty()156 int32_t RdbSmsMmsHelper::DeleteDataByThirty()
157 {
158     int changedRows = 0;
159     int32_t result;
160     std::string values;
161     std::string date;
162     GetTimeOfThirty(date);
163     values.append(SmsMmsInfo::START_TIME).append("< '").append(date).append("'");
164     result = Delete(changedRows, TABLE_SMS_MMS_INFO, values);
165     return result;
166 }
167 
InsertSmsMmsInfo(int64_t & id,const NativeRdb::ValuesBucket & values)168 int RdbSmsMmsHelper::InsertSmsMmsInfo(int64_t &id, const NativeRdb::ValuesBucket &values)
169 {
170     return Insert(id, values, TABLE_SMS_MMS_INFO);
171 }
172 
BatchInsertSmsMmsInfo(int64_t & id,const std::vector<NativeRdb::ValuesBucket> & values)173 int32_t RdbSmsMmsHelper::BatchInsertSmsMmsInfo(int64_t &id, const std::vector<NativeRdb::ValuesBucket> &values)
174 {
175     int32_t result = BeginTransaction();
176     if (result != NativeRdb::E_OK) {
177         DATA_STORAGE_LOGE("RdbSmsMmsHelper::BatchInsertSmsMmsInfo BeginTransaction is error!");
178         return result;
179     }
180     for (const NativeRdb::ValuesBucket &item : values) {
181         result = InsertSmsMmsInfo(id, item);
182         if (result != NativeRdb::E_OK) {
183             DATA_STORAGE_LOGE("RdbSmsMmsHelper::InsertSmsMmsInfo error result = %{public}d", result);
184             RollBack();
185             return result;
186         }
187     }
188     result = CommitTransactionAction();
189     return result;
190 }
191 
CommitTransactionAction()192 int RdbSmsMmsHelper::CommitTransactionAction()
193 {
194     int result = Commit();
195     if (result != NativeRdb::E_OK) {
196         RollBack();
197     }
198     return result;
199 }
200 
QueryMaxGroupId()201 std::unique_ptr<NativeRdb::AbsSharedResultSet> RdbSmsMmsHelper::QueryMaxGroupId()
202 {
203     std::string sql;
204     std::string maxGroupId("maxGroupId");
205     sql.append("select MAX(").append(SmsMmsInfo::GROUP_ID).append(") as ");
206     sql.append(maxGroupId).append(" from ") .append(TABLE_SMS_MMS_INFO);
207     return QuerySql(sql);
208 }
209 
StatisticsUnRead()210 std::unique_ptr<NativeRdb::AbsSharedResultSet> RdbSmsMmsHelper::StatisticsUnRead()
211 {
212     std::string sql;
213     sql.append("select count(*) as totalListCount, ");
214     sql.append("count(CASE WHEN ").append(SmsMmsInfo::MSG_TYPE).append("=0 THEN 1 ELSE null END) as unreadCount, ");
215     sql.append("count(CASE WHEN ").append(SmsMmsInfo::MSG_TYPE).append("=1 THEN 1 ELSE null END) as unreadTotalOfInfo");
216     sql.append(" from ").append(TABLE_SMS_MMS_INFO).append(" WHERE ").append(SmsMmsInfo::IS_READ).append("=0");
217     return QuerySql(sql);
218 }
219 } // namespace Telephony
220 } // namespace OHOS
221