README.md
1# Data Storage<a name="EN-US_TOPIC_0000001152064139"></a>
2
3- [Introduction](#section117mcpsimp)
4- [Directory Structure](#section124mcpsimp)
5- [Constraints](#section128mcpsimp)
6- [Usage](#section134mcpsimp)
7 - [Available APIs](#section136mcpsimp)
8 - [Permissions]( #section137mcpsimp)
9
10- [Usage Guidelines](#section163mcpsimp)
11 - [Parameters of the Insert API](#section1099113151207)
12 - [Parameters of the Delete API](#section1098113151208)
13 - [Parameters of the Update API](#section1099113151207)
14 - [Parameters of the Query API](#section1096113151208)
15 - [Sample Code](#section1558565082915)
16
17- [Repositories Involved](#section206mcpsimp)
18
19## Introduction<a name="section117mcpsimp"></a>
20
21The data storage module stores persistent data of key modules of the Telephony subsystem, such as the SIM cards and SMS modules, and provides the **DataShare** API for data access.
22
23**Figure 1** Architecture of the data storage module<a name="fig13267152558"></a>
24
25
26
27## Directory Structure<a name="section124mcpsimp"></a>
28
29```
30/base/telephony/data_storage # Data storage
31├─ BUILD.gn # Build script (gn)
32├─ README.md # Readme
33├─ common # Public and common files
34│ ├─ include
35│ └─ src
36├─ opkey # opkey framework
37│ ├─ include
38│ └─ src
39├─ pdp_profile # Network carrier
40│ ├─ include
41│ └─ src
42├─ sim # SIM card
43│ ├─ include
44│ └─ src
45├─ sms_mms # SMS/MMS
46│ ├─ include
47│ └─ src
48├─ ohos.build # Build code
49└─ test # Test code
50```
51
52## Constraints<a name="section128mcpsimp"></a>
53
54- Programming language: C++
55
56- Software constraints: This module must work with the Utils and Application Framework subsystems.
57
58- Hardware constraints: none
59
60- Use case: When a user needs to edit the persistent data stored in Telephony subsystem modules, such as the SIM card and SMS/MMS modules, your application can call the **Insert**, **Delete**, **Update**, and **Query** APIs provided by **DataShareHelper** as demanded.
61
62 You need to declare the corresponding permission for access to the specified URI.
63
64## Usage<a name="section134mcpsimp"></a>
65
66### Available APIs<a name="section136mcpsimp"></a>
67
68**Table 1** APIs provided by DataShareHelper
69
70<a name="table165976561598"></a>
71
72| API Definition | **Description**|
73| ------------------------------------------------------------ | ------------ |
74| int Insert(Uri &uri, const DataShare::DataShareValuesBucket &value) | Inserts data. |
75| int Delete(const Uri &uri, const DataShare::DataSharePredicates &predicates) | Deletes data. |
76| int Update(Uri &uri, const DataSharePredicates &predicates, const DataShareValuesBucket &value) | Updates data. |
77| std::shared_ptr\<DataShare::DataShareResultSet\> Query(Uri &uri, const DataSharePredicates &predicates, std::vector\<std::string\> &columns) | Queries data. |
78
79**Table 2** Required permissions
80
81<a name="table165976561598"></a>
82
83| Module | **Required Permission** |
84| -------------- | ------------------------------------------------------------ |
85| SMS/MMS module | ohos.permission.READ_MESSAGES |
86| SIM card module | ohos.permission.GET_TELEPHONY_STATE<br>ohos.permission.SET_TELEPHONY_STATE |
87| Network carrier module| ohos.permission.GET_TELEPHONY_STATE<br>ohos.permission.SET_TELEPHONY_STATE |
88| opkey module| ohos.permission.GET_TELEPHONY_STATE<br>ohos.permission.SET_TELEPHONY_STATE |
89
90## Usage Guidelines<a name="section163mcpsimp"></a>
91
92### Parameters of the Insert API<a name="section1099113151207"></a>
93
94**Table 3** Parameters of the Insert API
95
96<a name="table1234838197"></a>
97
98| Parameter | **Description** |
99| ----- | ------------------------------------- |
100| uri | Resource path. |
101| value | Data set. This field corresponds to the table structure field on which the current operation is performed.|
102
103### Parameters of the Delete API<a name="section1098113151208"></a>
104
105**Table 4** Parameters of the Delete API
106
107<a name="table1234838197"></a>
108
109| Parameter | Description |
110| ---------- | ------------------------------------- |
111| uri | Resource path. |
112| predicates | Conditions for data deletion. |
113
114### Parameters of the Update API<a name="section1097113151210"></a>
115
116**Table 5** Parameters of the Update API
117
118<a name="table1234838197"></a>
119
120| Parameter | Description |
121| ---------- | -------- |
122| uri | Resource path.|
123| predicates | Conditions for data updating.|
124| value | Data set. This field corresponds to the table structure field on which the current operation is performed.|
125
126### Parameters of the Query API<a name="section1096113151208"></a>
127
128**Table 6** Parameters of the Query API
129
130<a name="table1234838197"></a>
131
132| Parameter | Description |
133| ---------- | -------------- |
134| uri | Resource path. |
135| predicates | Conditions for data query. |
136| columns | Fields in the query result.|
137
138### Sample Code<a name="section1558565082915"></a>
139
140The following provides the procedure and sample code for you to query, insert, delete, or update SMS/MMS data:
141
1421. Call **SystemAbilityManagerClient** to obtain a **SystemAbilityManager** object.
1432. Call **saManager** to obtain an **IRemoteObject** object based on the specified service ID.
1443. Call **IRemoteObject** to create a **DataShareHelper** object.
1454. Call **DataShareHelper::Query** to query data, and call the other related APIs to process data.
146
147 Sample code for creating a **DataShareHelper** instance:
148 ```
149 std::shared_ptr<DataShare::DataShareHelper> DataStorageGtest::CreateDataShareHelper(
150 int32_t systemAbilityId, std::string &uri)
151 {
152 // Obtain a SystemAbilityManager instance through SystemAbilityManagerClient.
153 auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
154 if (saManager == nullptr) {
155 DATA_STORAGE_LOGE("DataSimRdbHelper Get system ability mgr failed.");
156 return nullptr;
157 }
158 // Obtain an IRemoteObject.
159 auto remoteObj = saManager->GetSystemAbility(systemAbilityId);
160 while (remoteObj == nullptr) {
161 DATA_STORAGE_LOGE("DataSimRdbHelper GetSystemAbility Service Failed.");
162 return nullptr;
163 }
164 // Create a DataShareHelper instance.
165 return DataShare::DataShareHelper::Creator(remoteObj, uri);
166 }
167 ```
168 Sample code for querying SMS/MMS messages:
169 ```
170 std::shared_ptr<DataShare::DataShareResultSet> DataStorageGtest::SmsSelect(const std::shared_ptr<DataShare::DataShareHelper> &helper)
171 {
172 // Resource path
173 Uri uri("datashare:///com.ohos.smsmmsability/sms_mms/sms_mms_info");
174 // Fields in the query result
175 std::vector<std::string> columns;
176 // Phone number of the message sender
177 columns.push_back("sender_number");
178 // Message title
179 columns.push_back("msg_title");
180 // Message content
181 columns.push_back("msg_content");
182 // Query predicates
183 DataShare::DataSharePredicates predicates;
184 // Call the DataShareHelper::Query API to query data.
185 return helper->Query(uri, predicates, columns);
186 }
187 ```
188 Sample code for inserting SMS/MMS messages:
189 ```
190 int SmsInsert(std::shared_ptr<DataShare::DataShareHelper> &helper)
191 {
192 Uri uri("datashare:///com.ohos.smsmmsability/sms_mms/sms_mms_info");
193 DataShare::DataShareValuesBucket value;
194 // Phone number of the message recipient
195 value.Put(SmsMmsInfo::RECEIVER_NUMBER, "138XXXXXXXX");
196 // Message content
197 value.Put(SmsMmsInfo::MSG_CONTENT, "ceshi");
198 value.Put(SmsMmsInfo::GROUP_ID, 1);
199 return helper->Insert(uri, value);
200 }
201 ```
202 Sample code for deleting SMS/MMS messages:
203 ```
204 int SmsDelete(std::shared_ptr<DataShare::DataShareHelper> helper)
205 {
206 Uri uri("datashare:///com.ohos.smsmmsability/sms_mms/sms_mms_info");
207 DataShare::DataSharePredicates predicates;
208 // Delete the message whose MSG_ID is 1.
209 predicates.EqualTo(SmsMmsInfo::MSG_ID, "1");
210 return helper->Delete(uri, predicates);
211 }
212 ```
213 Sample code for updating SMS/MMS messages:
214 ```
215 int SmsUpdate(std::shared_ptr<DataShare::DataShareHelper> helper)
216 {
217 Uri uri("datashare:///com.ohos.smsmmsability/sms_mms/sms_mms_info");
218 DataShare::DataShareValuesBucket values;
219 // Message content
220 values.Put(SmsMmsInfo::MSG_CONTENT, "hi ohos");
221 DataShare::DataSharePredicates predicates;
222 // Message ID
223 predicates.EqualTo(SmsMmsInfo::MSG_ID, "1");
224 return helper->Update(uri, predicates, values);
225 }
226 ```
227
228
229## Repositories Involved<a name="section206mcpsimp"></a>
230
231[Telephony Subsystem](https://gitee.com/openharmony/docs/blob/master/en/readme/telephony.md)
232
233**telephony_data_storage**
234
235[telephony_core_service](https://gitee.com/openharmony/telephony_core_service/blob/master/README.md)
236
237[telephony_sms_mms](https://gitee.com/openharmony/telephony_sms_mms/blob/master/README.md)
238
239[telephony_cellular_call](https://gitee.com/openharmony/telephony_cellular_call/blob/master/README.md)
240
241[telephony_call_manager](https://gitee.com/openharmony/telephony_call_manager/blob/master/README.md)
242
README_zh.md
1# 数据库及持久化<a name="ZH-CN_TOPIC_0000001152064139"></a>
2
3- [简介](#section117mcpsimp)
4- [目录](#section124mcpsimp)
5- [约束](#section128mcpsimp)
6- [接口说明](#section136mcpsimp)
7
8- [使用说明](#section163mcpsimp)
9 - [插入接口参数说明](#section1099113151207)
10 - [删除接口参数说明](#section1098113151208)
11 - [更新接口参数说明](#section1099113151207)
12 - [查询接口参数说明](#section1096113151208)
13 - [接口调用代码示例](#section1558565082915)
14
15- [相关仓](#section206mcpsimp)
16
17## 简介<a name="section117mcpsimp"></a>
18
19数据库及持久化模块负责电话服务子系统中的SIM卡/短彩信等模块持久化数据存储,提供DataShare访问接口。
20
21**图 1** 数据库及持久化架构图<a name="fig13267152558"></a>
22
23
24
25## 目录<a name="section124mcpsimp"></a>
26
27```
28/base/telephony/data_storage # 数据库及持久化
29├─ common # 通用文件
30│ ├─ include # 头文件目录
31│ └─ src # 实现代码目录
32├─ figures # Readme资源文件
33├─ opkey # 随卡框架
34│ ├─ include # 头文件目录
35│ └─ src # 实现代码目录
36├─ pdp_profile # 网络运营商
37│ ├─ include # 头文件目录
38│ └─ src # 实现代码目录
39├─ signature # 签名文件
40├─ sim # sim卡
41│ ├─ include # 头文件目录
42│ └─ src # 实现代码目录
43├─ sms_mms # 短彩信
44│ ├─ include # 头文件目录
45│ └─ src # 实现代码目录
46└─ test # 测试相关
47 └── unit_test # 单元测试相关代码
48```
49
50## 约束<a name="section128mcpsimp"></a>
51
52- 开发语言:C++
53
54- 软件约束:需要与以下服务配合使用:公共基础子库系统,应用框架子系统。
55
56- 硬件约束:无
57
58- 使用场景:当用户需要获取电话服务子系统中的SIM卡/短彩信等模块持久化数据时,可通过DataShareHelper提供的增/删/改/查接口来获取数据。
59
60 访问时需要提供对应的权限和URI。
61
62### 接口说明<a name="section136mcpsimp"></a>
63
64**表 1** 增/删/改/查接口
65
66<a name="table165976561598"></a>
67
68| 接口定义 | **接口描述** |
69| ------------------------------------------------------------ | ------------ |
70| int Insert(Uri &uri, const DataShare::DataShareValuesBucket &value) | 插入数据 |
71| int Delete(const Uri &uri, const DataShare::DataSharePredicates &predicates) | 删除数据 |
72| int Update(Uri &uri, const DataSharePredicates &predicates, const DataShareValuesBucket &value) | 更新数据 |
73| std::shared_ptr\<DataShare::DataShareResultSet\> Query(Uri &uri, const DataSharePredicates &predicates, std::vector\<std::string\> &columns) | 查询数据 |
74
75**表 2** 权限说明
76
77<a name="table165976561598"></a>
78
79| 模块 | **所需权限** |
80| -------------- | ------------------------------------------------------------ |
81| 短彩信模块 | ohos.permission.READ_MESSAGES |
82| SIM卡模块 | ohos.permission.GET_TELEPHONY_STATE<br />ohos.permission.SET_TELEPHONY_STATE |
83| 网络运营商模块 | ohos.permission.GET_TELEPHONY_STATE<br />ohos.permission.SET_TELEPHONY_STATE |
84| 随卡框架模块 | ohos.permission.GET_TELEPHONY_STATE<br />ohos.permission.SET_TELEPHONY_STATE |
85
86## 使用说明<a name="section163mcpsimp"></a>
87
88### 插入接口参数说明<a name="section1099113151207"></a>
89
90**表 3** Insert接口参数说明
91
92<a name="table1234838197"></a>
93
94| 参数 | **说明** |
95| ----- | ------------------------------------- |
96| uri | 资源路径 |
97| value | 数据集合,字段对应当前操作的表结构字段 |
98
99### 删除接口参数说明<a name="section1098113151208"></a>
100
101**表 4** Delete接口参数说明
102
103<a name="table1234838197"></a>
104
105| 参数 | 说明 |
106| ---------- | ------------------------------------- |
107| uri | 资源路径 |
108| predicates | 删除条件 |
109
110### 更新接口参数说明<a name="section1097113151210"></a>
111
112**表 5** Update接口参数说明
113
114<a name="table1234838197"></a>
115
116| 参数 | 说明 |
117| ---------- | -------- |
118| uri | 资源路径 |
119| predicates | 更新条件 |
120| value | 数据集合,字段对应当前操作的表结构字段 |
121
122### 查询接口参数说明<a name="section1096113151208"></a>
123
124**表 6** Query接口参数说明
125
126<a name="table1234838197"></a>
127
128| 参数 | 说明 |
129| ---------- | -------------- |
130| uri | 资源路径 |
131| predicates | 查询条件 |
132| columns | 查询返回的字段 |
133
134### 接口调用代码示例<a name="section1558565082915"></a>
135
136以查询/插入/删除/更新短彩信数据为例,主要步骤和代码如下:
137
1381. 使用SystemAbilityManagerClient获得SystemAbilityManager对象。
1392. 使用saManager获得指定服务Id的IRemoteObject对象。
1403. 使用IRemoteObject创建DataShareHelper对象。
1414. 调用DataShareHelper::Query接口访问,并接收处理返回的数据。
142
143 创建DataShareHelper:
144 ```
145 std::shared_ptr<DataShare::DataShareHelper> DataStorageGtest::CreateDataShareHelper(
146 int32_t systemAbilityId, std::string &uri)
147 {
148 //通过SystemAbilityManagerClient获得SystemAbilityManager
149 auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
150 if (saManager == nullptr) {
151 DATA_STORAGE_LOGE("DataSimRdbHelper Get system ability mgr failed.");
152 return nullptr;
153 }
154 // 获得IRemoteObject
155 auto remoteObj = saManager->GetSystemAbility(systemAbilityId);
156 while (remoteObj == nullptr) {
157 DATA_STORAGE_LOGE("DataSimRdbHelper GetSystemAbility Service Failed.");
158 return nullptr;
159 }
160 // 创建DataShareHelper
161 return DataShare::DataShareHelper::Creator(remoteObj, uri);
162 }
163 ```
164 查询短彩信信息:
165 ```
166 std::shared_ptr<DataShare::DataShareResultSet> DataStorageGtest::SmsSelect(const std::shared_ptr<DataShare::DataShareHelper> &helper)
167 {
168 // 资源路径
169 Uri uri("datashare:///com.ohos.smsmmsability/sms_mms/sms_mms_info");
170 //查询返回的字段
171 std::vector<std::string> columns;
172 // 发送者号码
173 columns.push_back("sender_number");
174 // 消息标题
175 columns.push_back("msg_title");
176 // 消息内容
177 columns.push_back("msg_content");
178 // 查询谓词
179 DataShare::DataSharePredicates predicates;
180 // 调用DataShareHelper::Query接口查询
181 return helper->Query(uri, predicates, columns);
182 }
183 ```
184 插入短彩信信息:
185 ```
186 int SmsInsert(std::shared_ptr<DataShare::DataShareHelper> &helper)
187 {
188 Uri uri("datashare:///com.ohos.smsmmsability/sms_mms/sms_mms_info");
189 DataShare::DataShareValuesBucket value;
190 // 接收者号码
191 value.Put(SmsMmsInfo::RECEIVER_NUMBER, "138XXXXXXXX");
192 // 消息内容
193 value.Put(SmsMmsInfo::MSG_CONTENT, "ceshi");
194 value.Put(SmsMmsInfo::GROUP_ID, 1);
195 return helper->Insert(uri, value);
196 }
197 ```
198 删除短彩信信息:
199 ```
200 int SmsDelete(std::shared_ptr<DataShare::DataShareHelper> helper)
201 {
202 Uri uri("datashare:///com.ohos.smsmmsability/sms_mms/sms_mms_info");
203 DataShare::DataSharePredicates predicates;
204 // 删除MSG_ID为1的信息
205 predicates.EqualTo(SmsMmsInfo::MSG_ID, "1");
206 return helper->Delete(uri, predicates);
207 }
208 ```
209 更新短彩信信息:
210 ```
211 int SmsUpdate(std::shared_ptr<DataShare::DataShareHelper> helper)
212 {
213 Uri uri("datashare:///com.ohos.smsmmsability/sms_mms/sms_mms_info");
214 DataShare::DataShareValuesBucket values;
215 // 信息内容
216 values.Put(SmsMmsInfo::MSG_CONTENT, "hi ohos");
217 DataShare::DataSharePredicates predicates;
218 // 信息Id
219 predicates.EqualTo(SmsMmsInfo::MSG_ID, "1");
220 return helper->Update(uri, predicates, values);
221 }
222 ```
223
224
225## 相关仓<a name="section206mcpsimp"></a>
226
227[电话服务子系统](https://gitee.com/openharmony/docs/blob/master/zh-cn/readme/电话服务子系统.md)
228
229**telephony_data_storage**
230
231[telephony_core_service](https://gitee.com/openharmony/telephony_core_service/blob/master/README_zh.md)
232
233[telephony_sms_mms](https://gitee.com/openharmony/telephony_sms_mms/blob/master/README_zh.md)
234
235[telephony_cellular_call](https://gitee.com/openharmony/telephony_cellular_call/blob/master/README_zh.md)
236
237[telephony_call_manager](https://gitee.com/openharmony/telephony_call_manager/blob/master/README_zh.md)
238
239