提交 07afdeb3 编写于 作者: A Annie_wang

update docs

Signed-off-by: NAnnie_wang <annie.wangli@huawei.com>
上级 e7a61f77
...@@ -65,6 +65,7 @@ The following table lists the APIs used for RDB data persistence. Most of the AP ...@@ -65,6 +65,7 @@ The following table lists the APIs used for RDB data persistence. Most of the AP
securityLevel: relationalStore.SecurityLevel.S1 // Database security level. securityLevel: relationalStore.SecurityLevel.S1 // Database security level.
}; };
// The current RDB store version is 3, and the table structure is EMPLOYEE (NAME, AGE, SALARY, CODES).
const SQL_CREATE_TABLE ='CREATE TABLE IF NOT EXISTS EMPLOYEE (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT NOT NULL, AGE INTEGER, SALARY REAL, CODES BLOB)'; // SQL statement for creating a data table. const SQL_CREATE_TABLE ='CREATE TABLE IF NOT EXISTS EMPLOYEE (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT NOT NULL, AGE INTEGER, SALARY REAL, CODES BLOB)'; // SQL statement for creating a data table.
relationalStore.getRdbStore(this.context, STORE_CONFIG, (err, store) => { relationalStore.getRdbStore(this.context, STORE_CONFIG, (err, store) => {
...@@ -73,7 +74,28 @@ The following table lists the APIs used for RDB data persistence. Most of the AP ...@@ -73,7 +74,28 @@ The following table lists the APIs used for RDB data persistence. Most of the AP
return; return;
} }
console.info(`Succeeded in getting RdbStore.`); console.info(`Succeeded in getting RdbStore.`);
// When the RDB store is created, the default version is 0.
if (store.version == 0) {
store.executeSql(SQL_CREATE_TABLE); // Create a data table. store.executeSql(SQL_CREATE_TABLE); // Create a data table.
// Set the RDB store version, which must be an integer greater than 0.
store.version = 3;
}
// If the RDB store version is not 0 and does not match the current version, upgrade or downgrade the RDB store.
// For example, upgrade the RDB store from version 1 to version 2.
if (store.version != 3 && store.version == 1) {
// Upgrade the RDB store from version 1 to version 2, and change the table structure from EMPLOYEE (NAME, SALARY, CODES, ADDRESS) to EMPLOYEE (NAME, AGE, SALARY, CODES, ADDRESS).
store.executeSql("ALTER TABLE EMPLOYEE ADD COLUMN AGE INTEGER", null);
store.version = 2;
}
// For example, upgrade the RDB store from version 2 to version 3.
if (store.version != 3 && store.version == 2) {
// Upgrade the RDB store from version 2 to version 3, and change the table structure from EMPLOYEE (NAME, AGE, SALARY, CODES, ADDRESS) to EMPLOYEE (NAME, AGE, SALARY, CODES).
store.executeSql("ALTER TABLE EMPLOYEE DROP COLUMN ADDRESS TEXT", null);
store.version = 3;
}
// Perform operations such as adding, deleting, modifying, and querying data in the RDB store. // Perform operations such as adding, deleting, modifying, and querying data in the RDB store.
...@@ -97,6 +119,7 @@ The following table lists the APIs used for RDB data persistence. Most of the AP ...@@ -97,6 +119,7 @@ The following table lists the APIs used for RDB data persistence. Most of the AP
securityLevel: relationalStore.SecurityLevel.S1 // Database security level. securityLevel: relationalStore.SecurityLevel.S1 // Database security level.
}; };
// The RDB store version is 3, and the table structure is EMPLOYEE (NAME, AGE, SALARY, CODES).
const SQL_CREATE_TABLE ='CREATE TABLE IF NOT EXISTS EMPLOYEE (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT NOT NULL, AGE INTEGER, SALARY REAL, CODES BLOB)'; // SQL statement for creating a data table. const SQL_CREATE_TABLE ='CREATE TABLE IF NOT EXISTS EMPLOYEE (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT NOT NULL, AGE INTEGER, SALARY REAL, CODES BLOB)'; // SQL statement for creating a data table.
relationalStore.getRdbStore(context, STORE_CONFIG, (err, store) => { relationalStore.getRdbStore(context, STORE_CONFIG, (err, store) => {
...@@ -105,7 +128,28 @@ The following table lists the APIs used for RDB data persistence. Most of the AP ...@@ -105,7 +128,28 @@ The following table lists the APIs used for RDB data persistence. Most of the AP
return; return;
} }
console.info(`Succeeded in getting RdbStore.`); console.info(`Succeeded in getting RdbStore.`);
// When the RDB store is created, the default version is 0.
if (store.version == 0) {
store.executeSql(SQL_CREATE_TABLE); // Create a data table. store.executeSql(SQL_CREATE_TABLE); // Create a data table.
// Set the RDB store version, which must be an integer greater than 0.
store.version = 3;
}
// If the RDB store version is not 0 and does not match the current version, upgrade or downgrade the RDB store.
// For example, upgrade the RDB store from version 1 to version 2.
if (store.version != 3 && store.version == 1) {
// Upgrade the RDB store from version 1 to version 2, and change the table structure from EMPLOYEE (NAME, SALARY, CODES, ADDRESS) to EMPLOYEE (NAME, AGE, SALARY, CODES, ADDRESS).
store.executeSql("ALTER TABLE EMPLOYEE ADD COLUMN AGE INTEGER", null);
store.version = 2;
}
// For example, upgrade the RDB store from version 2 to version 3.
if (store.version != 3 && store.version == 2) {
// Upgrade the RDB store from version 2 to version 3, and change the table structure from EMPLOYEE (NAME, AGE, SALARY, CODES, ADDRESS) to EMPLOYEE (NAME, AGE, SALARY, CODES).
store.executeSql("ALTER TABLE EMPLOYEE DROP COLUMN ADDRESS TEXT", null);
store.version = 3;
}
// Perform operations such as adding, deleting, modifying, and querying data in the RDB store. // Perform operations such as adding, deleting, modifying, and querying data in the RDB store.
...@@ -185,13 +229,22 @@ The following table lists the APIs used for RDB data persistence. Most of the AP ...@@ -185,13 +229,22 @@ The following table lists the APIs used for RDB data persistence. Most of the AP
```js ```js
let predicates = new relationalStore.RdbPredicates('EMPLOYEE'); let predicates = new relationalStore.RdbPredicates('EMPLOYEE');
predicates.equalTo('NAME', 'Rose'); predicates.equalTo('NAME', 'Rose');
store.query(predicates, ['ID', 'NAME', 'AGE', 'SALARY', 'CODES'], (err, resultSet) => { store.query(predicates, ['ID', 'NAME', 'AGE', 'SALARY'], (err, resultSet) => {
if (err) { if (err) {
console.error(`Failed to query data. Code:${err.code}, message:${err.message}`); console.error(`Failed to query data. Code:${err.code}, message:${err.message}`);
return; return;
} }
console.info(`ResultSet column names: ${resultSet.columnNames}`); console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`);
console.info(`ResultSet column count: ${resultSet.columnCount}`); // resultSet is a cursor of a data set. By default, the cursor points to the -1st record. Valid data starts from 0.
while (resultSet.goToNextRow()) {
const id = resultSet.getLong(resultSet.getColumnIndex("ID"));
const name = resultSet.getString(resultSet.getColumnIndex("NAME"));
const age = resultSet.getLong(resultSet.getColumnIndex("AGE"));
const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY"));
console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`);
}
// Release the data set memory.
resultSet.close();
}) })
``` ```
...@@ -221,7 +274,7 @@ The following table lists the APIs used for RDB data persistence. Most of the AP ...@@ -221,7 +274,7 @@ The following table lists the APIs used for RDB data persistence. Most of the AP
console.info('Succeeded in deleting RdbStore.'); console.info('Succeeded in deleting RdbStore.');
}); });
} }
} }
``` ```
FA model: FA model:
......
# @ohos.data.relationalStore (RDB Store) # @ohos.data.relationalStore (RDB Store)
The relational database (RDB) store manages data based on relational models. With the underlying SQLite database, the RDB store provides a complete mechanism for managing local databases. To satisfy different needs in complicated scenarios, the RDB store offers a series of APIs for performing operations such as adding, deleting, modifying, and querying data, and supports direct execution of SQL statements. The worker threads are not supported. The relational database (RDB) store manages data based on relational models. The RDB store provides a complete mechanism for managing local databases based on the underlying SQLite. To satisfy different needs in complicated scenarios, the RDB store offers a series of APIs for performing operations such as adding, deleting, modifying, and querying data, and supports direct execution of SQL statements. The worker threads are not supported.
The **relationalStore** module provides the following functions: The **relationalStore** module provides the following functions:
...@@ -323,7 +323,7 @@ Enumerates the RDB store security levels. ...@@ -323,7 +323,7 @@ Enumerates the RDB store security levels.
> **NOTE** > **NOTE**
> >
> To perform data synchronization operations, the RDB store security level must be lower than or equal to that of the peer device. For details, see the [Cross-Device Data Synchronization Mechanism]( ../../database/sync-app-data-across-devices-overview.md#cross-device-data-synchronization-mechanism). > To perform data synchronization operations, the RDB store security level must be lower than or equal to that of the peer device. For details, see the [Access Control Mechanism in Cross-Device Synchronization]( ../../database/access-control-by-device-and-data-level.md#access-control-mechanism-in-cross-device-synchronization).
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core **System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
...@@ -334,6 +334,45 @@ Enumerates the RDB store security levels. ...@@ -334,6 +334,45 @@ Enumerates the RDB store security levels.
| S3 | 3 | The RDB store security level is high. If data leakage occurs, major impact will be caused on the database. For example, an RDB store that contains information such as user fitness, health, and location data.| | S3 | 3 | The RDB store security level is high. If data leakage occurs, major impact will be caused on the database. For example, an RDB store that contains information such as user fitness, health, and location data.|
| S4 | 4 | The RDB store security level is critical. If data leakage occurs, severe impact will be caused on the database. For example, an RDB store that contains information such as authentication credentials and financial data.| | S4 | 4 | The RDB store security level is critical. If data leakage occurs, severe impact will be caused on the database. For example, an RDB store that contains information such as authentication credentials and financial data.|
## AssetStatus<sup>10+</sup>
Enumerates the asset statuses. Use the enum names instead of the enum values.
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
| Name | Value | Description |
| ------------------------------- | --- | -------------- |
| ASSET_NORMAL | - | The asset is in normal status. |
| ASSET_INSERT | - | The asset is to be inserted to the cloud.|
| ASSET_UPDATE | - | The asset is to be updated to the cloud.|
| ASSET_DELETE | - | The asset is to be deleted from the cloud.|
| ASSET_ABNORMAL | - | The asset is in abnormal status. |
| ASSET_DOWNLOADING | - | The asset is being downloaded to a local device.|
## Asset<sup>10+</sup>
Defines information about an asset (such as a document, image, and video). The asset APIs do not support **Datashare**.
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
| Name | Type | Mandatory | Description |
| ----------- | --------------------------- | --- | ------------ |
| name | string | Yes | Asset name. |
| uri | string | Yes | Asset URI, which is an absolute path in the system. |
| path | string | Yes | Application sandbox path of the asset. |
| createTime | string | Yes | Time when the asset was created. |
| modifyTime | string | Yes | Time when the asset was last modified.|
| size | string | Yes | Size of the asset. |
| status | [AssetStatus](#assetstatus10) | No | Asset status. The default value is **ASSET_NORMAL**. |
## Assets<sup>10+</sup>
Defines an array of the [Asset](#asset10) type.
| Type | Description |
| ------- | -------------------- |
| [Asset](#asset10)[] | Array of assets. |
## ValueType ## ValueType
Defines the data types allowed. Defines the data types allowed.
...@@ -342,9 +381,13 @@ Defines the data types allowed. ...@@ -342,9 +381,13 @@ Defines the data types allowed.
| Type | Description | | Type | Description |
| ------- | -------------------- | | ------- | -------------------- |
| null<sup>10+</sup> | Null. |
| number | Number. | | number | Number. |
| string | String. | | string | String. |
| boolean | Boolean.| | boolean | Boolean.|
| Uint8Array<sup>10+</sup> | Uint8 array. |
| Asset<sup>10+</sup> | [Asset](#asset10). |
| Assets<sup>10+</sup> | [Assets](#assets10).|
## ValuesBucket ## ValuesBucket
...@@ -353,32 +396,80 @@ Defines the types of the key and value in a KV pair. This type is not multi-thre ...@@ -353,32 +396,80 @@ Defines the types of the key and value in a KV pair. This type is not multi-thre
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core **System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
| Key Type| Value Type | | Key Type| Value Type |
| ------ | ----------------------------------------------------------- | | ------ | ----------------------- |
| string | [ValueType](#valuetype)\|&nbsp;Uint8Array&nbsp;\|&nbsp;null | | string | [ValueType](#valuetype) |
## SyncMode ## SyncMode
Defines the database synchronization mode. Enumerates the database synchronization modes.
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
| Name | Value | Description | | Name | Value | Description |
| -------------- | ---- | ---------------------------------- | | -------------- | ---- | ---------------------------------- |
| SYNC_MODE_PUSH | 0 | Data is pushed from a local device to a remote device.| | SYNC_MODE_PUSH | 0 | Push data from a local device to a remote device.<br>**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core |
| SYNC_MODE_PULL | 1 | Data is pulled from a remote device to a local device. | | SYNC_MODE_PULL | 1 | Pull data from a remote device to a local device.<br>**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core |
| SYNC_MODE_TIME_FIRST<sup>10+</sup> | - | Synchronize with the data with the latest modification time. Use the enum names instead of the enum values. Currently, manual synchronization between the device and cloud is not supported.<br>**System capability**: SystemCapability.DistributedDataManager.CloudSync.Client|
| SYNC_MODE_NATIVE_FIRST<sup>10+</sup> | - | Synchronize data from a local device to the cloud. Use the enum names instead of the enum values. Currently, manual synchronization between the device and cloud is not supported.<br>**System capability**: SystemCapability.DistributedDataManager.CloudSync.Client |
| SYNC_MODE_CLOUD_FIRST<sup>10+</sup> | - | Synchronize data from the cloud to a local device. Use the enum names instead of the enum values. Currently, manual synchronization between the device and cloud is not supported.<br>**System capability**: SystemCapability.DistributedDataManager.CloudSync.Client |
## SubscribeType ## SubscribeType
Defines the subscription type. Enumerates the subscription types.
**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC **Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
| Name | Value | Description |
| --------------------- | ---- | ------------------ |
| SUBSCRIBE_TYPE_REMOTE | 0 | Subscribe to remote data changes.<br>**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core|
| SUBSCRIBE_TYPE_CLOUD<sup>10+</sup> | - | Subscribe to cloud data changes. Use the enum names instead of the enum values.<br>**System capability**: SystemCapability.DistributedDataManager.CloudSync.Client|
| SUBSCRIBE_TYPE_CLOUD_DETAILS<sup>10+</sup> | - | Subscribe to cloud data change details. Use the enum names instead of the enum values.<br>**System capability**: SystemCapability.DistributedDataManager.CloudSync.Client|
## ChangeType<sup>10+</sup>
Enumerates data change types. Use the enum names instead of the enum values.
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core **System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
| Name | Value | Description | | Name | Value | Description |
| --------------------- | ---- | ------------------ | | -------------------------- | --- | -------------------------- |
| SUBSCRIBE_TYPE_REMOTE | 0 | Subscribe to remote data changes.| | DATA_CHANGE | - | Data change. |
| SUBSCRIBE_TYPE_CLOUD<sup>10+</sup> | 1 | Subscribe to cloud data changes.| | ASSET_CHANGE | - | Asset change.|
## ChangeInfo<sup>10+</sup>
Defines the details about the device-cloud synchronization process.
**System capability**: SystemCapability.DistributedDataManager.CloudSync.Client
| Name | Type | Mandatory | Description |
| -------- | ---------------------------------- | --- | -------------------------------------------------------------------------------------------------------------------- |
| table | string | Yes | Name of the table with data changes.<br>**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core |
| type | [ChangeType](#changetype10) | Yes | Type of the data changed, which can be data or asset.<br>**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core |
| inserted | Array\<string\> \| Array\<number\> | Yes | Location where data is inserted. If the primary key of the table is of the string type, the value is the value of the primary key. Otherwise, the value is the row number of the inserted data.<br>**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core |
| updated | Array\<string\> \| Array\<number\> | Yes | Location where data is updated. If the primary key of the table is of the string type, the value is the value of the primary key. Otherwise, the value is the row number of the updated data.<br>**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core |
| deleted | Array\<string\> \| Array\<number\> | Yes | Location where data is deleted. If the primary key of the table is of the string type, the value is the value of the primary key. Otherwise, the value is the row number of the deleted data.<br>**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core |
## DistributedType<sup>10+</sup>
Enumerates the distributed table types. Use the enum names instead of the enum values.
**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
| Name | Value | Description |
| ------------------ | --- | -------------------------------------------------------------------------------------------------- |
| DISTRIBUTED_DEVICE | - | Distributed database table between devices.<br>**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core |
| DISTRIBUTED_CLOUD | - | Distributed database table between the device and the cloud.<br>**System capability**: SystemCapability.DistributedDataManager.CloudSync.Client|
## DistributedConfig<sup>10+</sup>
Defines the configuration of the distributed mode of tables.
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
| Name | Type | Mandatory| Description |
| -------- | ------- | ---- | ------------------------------------------------------------ |
| autoSync | boolean | Yes | The value **true** means both automatic synchronization and manual synchronization are supported for the table. The value **false** means only manual synchronization is supported for the table.|
## ConflictResolution<sup>10+</sup> ## ConflictResolution<sup>10+</sup>
...@@ -2887,8 +2978,8 @@ Sets distributed tables. This API uses a promise to return the result. ...@@ -2887,8 +2978,8 @@ Sets distributed tables. This API uses a promise to return the result.
**Parameters** **Parameters**
| Name| Type | Mandatory| Description | | Name| Type | Mandatory| Description |
| ------ | ------------------- | ---- | ------------------------ | | ------ | ------------------------ | ---- | ------------------------ |
| tables | Array&lt;string&gt; | Yes | Names of the distributed tables to set.| | tables | ArrayArray&lt;string&gt; | Yes | Names of the distributed tables to set.|
**Return value** **Return value**
...@@ -2900,8 +2991,8 @@ Sets distributed tables. This API uses a promise to return the result. ...@@ -2900,8 +2991,8 @@ Sets distributed tables. This API uses a promise to return the result.
For details about the error codes, see [RDB Error Codes](../errorcodes/errorcode-data-rdb.md). For details about the error codes, see [RDB Error Codes](../errorcodes/errorcode-data-rdb.md).
| **ID**| **Error Message** | | **ID**| **Error Message**|
| ------------ | ---------------------------- | | ------------ | ------------ |
| 14800000 | Inner error. | | 14800000 | Inner error. |
**Example** **Example**
...@@ -2915,6 +3006,76 @@ promise.then(() => { ...@@ -2915,6 +3006,76 @@ promise.then(() => {
}) })
``` ```
### setDistributedTables<sup>10+</sup>
setDistributedTables(tables: Array&lt;string&gt;, type: number, config: DistributedConfig, callback: AsyncCallback&lt;void&gt;): void
Sets distributed tables. This API uses an asynchronous callback to return the result.
**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
**Parameters**
| Name | Type | Mandatory | Description |
| -------- | ----------------------------------- | --- | --------------- |
| tables | Array&lt;string&gt; | Yes | Names of the distributed tables to set. |
| type | number | Yes | Distributed type of the tables. Currently, only **relationalStore.DistributedType.DISTRIBUTED_DEVICE** and **relationalStore.DistributedType.DISTRIBUTED_CLOUD** are supported.<br> The value **relationalStore.DistributedType.DISTRIBUTED_DEVICE** indicates distributed tables across different devices.<br> The value **relationalStore.DistributedType.DISTRIBUTED_CLOUD** indicates distributed tables between the device and cloud.|
| config | [DistributedConfig](#distributedconfig10) | Yes| Configuration of the distributed mode.|
| callback | AsyncCallback&lt;void&gt; | Yes | Callback invoked to return the result.|
**Example**
```js
let config = new DistributedConfig();
config.autoSync = true;
store.setDistributedTables(["EMPLOYEE"], relationalStore.DistributedType.DISTRIBUTED_CLOUD, config, function (err) {
if (err) {
console.error(`SetDistributedTables failed, code is ${err.code},message is ${err.message}`);
return;
}
console.info(`SetDistributedTables successfully.`);
})
```
### setDistributedTables<sup>10+</sup>
setDistributedTables(tables: Array&lt;string>, type?: number, config?: DistributedConfig): Promise&lt;void>
Sets distributed tables. This API uses a promise to return the result.
**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
**Parameters**
| Name| Type | Mandatory| Description |
| ------ | ----------------------------------------- | ---- | ------------------------------------------------------------ |
| tables | Array&lt;string&gt; | Yes | Names of the distributed tables to set. |
| type | number | No | Distributed type of the tables. The default value is **relationalStore.DistributedType.DISTRIBUTED_DEVICE**.<br> Currently, only **relationalStore.DistributedType.DISTRIBUTED_DEVICE** and **relationalStore.DistributedType.DISTRIBUTED_CLOUD** are supported.<br> The value **relationalStore.DistributedType.DISTRIBUTED_DEVICE** indicates distributed tables across different devices.<br> The value **relationalStore.DistributedType.DISTRIBUTED_CLOUD** indicates distributed tables between the device and cloud.|
| config | [DistributedConfig](#distributedconfig10) | No | Configuration of the distributed mode. If this parameter is not specified, the value of **autoSync** is **false** by default, which means only manual synchronization is supported.|
**Return value**
| Type | Description |
| ------------------- | ------------------------- |
| Promise&lt;void&gt; | Promise that returns no value.|
**Example**
```js
let config = new DistributedConfig();
config.autoSync = true;
let promise = store.setDistributedTables(["EMPLOYEE"], relationalStore.DistributedType.DISTRIBUTED_CLOUD, config);
promise.then(() => {
console.info(`SetDistributedTables successfully.`);
}).catch((err) => {
console.error(`SetDistributedTables failed, code is ${err.code},message is ${err.message}`);
})
```
### obtainDistributedTableName ### obtainDistributedTableName
obtainDistributedTableName(device: string, table: string, callback: AsyncCallback&lt;string&gt;): void obtainDistributedTableName(device: string, table: string, callback: AsyncCallback&lt;string&gt;): void
...@@ -3045,7 +3206,7 @@ Synchronizes data between devices. This API uses an asynchronous callback to ret ...@@ -3045,7 +3206,7 @@ Synchronizes data between devices. This API uses an asynchronous callback to ret
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| ---------- | -------------------------------------------------- | ---- | ------------------------------------------------------------ | | ---------- | -------------------------------------------------- | ---- | ------------------------------------------------------------ |
| mode | [SyncMode](#syncmode) | Yes | Data synchronization mode. The value can be **push** or **pull**. | | mode | [SyncMode](#syncmode) | Yes | Data synchronization mode. The value can be **relationalStore.SyncMode.SYNC_MODE_PUSH** or **relationalStore.SyncMode.SYNC_MODE_PULL**. |
| predicates | [RdbPredicates](#rdbpredicates) | Yes | **RdbPredicates** object that specifies the data and devices to synchronize. | | predicates | [RdbPredicates](#rdbpredicates) | Yes | **RdbPredicates** object that specifies the data and devices to synchronize. |
| callback | AsyncCallback&lt;Array&lt;[string, number]&gt;&gt; | Yes | Callback invoked to send the synchronization result to the caller. <br>**string** indicates the device ID. <br>**number** indicates the synchronization status of that device. The value **0** indicates a successful synchronization. Other values indicate a synchronization failure. | | callback | AsyncCallback&lt;Array&lt;[string, number]&gt;&gt; | Yes | Callback invoked to send the synchronization result to the caller. <br>**string** indicates the device ID. <br>**number** indicates the synchronization status of that device. The value **0** indicates a successful synchronization. Other values indicate a synchronization failure. |
...@@ -3104,7 +3265,7 @@ Synchronizes data between devices. This API uses a promise to return the result. ...@@ -3104,7 +3265,7 @@ Synchronizes data between devices. This API uses a promise to return the result.
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| ---------- | ------------------------------------ | ---- | ------------------------------ | | ---------- | ------------------------------------ | ---- | ------------------------------ |
| mode | [SyncMode](#syncmode) | Yes | Data synchronization mode. The value can be **push** or **pull**.| | mode | [SyncMode](#syncmode) | Yes | Data synchronization mode. The value can be **relationalStore.SyncMode.SYNC_MODE_PUSH** or **relationalStore.SyncMode.SYNC_MODE_PULL**.|
| predicates | [RdbPredicates](#rdbpredicates) | Yes | **RdbPredicates** object that specifies the data and devices to synchronize. | | predicates | [RdbPredicates](#rdbpredicates) | Yes | **RdbPredicates** object that specifies the data and devices to synchronize. |
**Return value** **Return value**
...@@ -3157,7 +3318,38 @@ promise.then((result) =>{ ...@@ -3157,7 +3318,38 @@ promise.then((result) =>{
on(event: 'dataChange', type: SubscribeType, observer: Callback&lt;Array&lt;string&gt;&gt;): void on(event: 'dataChange', type: SubscribeType, observer: Callback&lt;Array&lt;string&gt;&gt;): void
Registers an observer for this RDB store. When the data in the RDB store changes, a callback is invoked to return the data changes. Registers the data change event listener for the RDB store. When the data in the RDB store changes, a callback is invoked to return the data changes.
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| event | string | Yes | Event to observe. The value is **dataChange**, which indicates a data change event. |
| type | [SubscribeType](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-data-relationalStore.md#subscribetype) | Yes | Subscription type to register. |
| observer | Callback&lt;Array&lt;string&gt;&gt; | Yes | Callback invoked to return the data change. **Array<string>** indicates the IDs of the peer devices whose data in the database is changed.|
**Example**
```
function storeObserver(devices) {
for (let i = 0; i < devices.length; i++) {
console.info(`device= ${devices[i]} data changed`);
}
}
try {
store.on('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver);
} catch (err) {
console.error(`Register observer failed, code is ${err.code},message is ${err.message}`);
}
```
### on('dataChange')<sup>10+</sup>
on(event: 'dataChange', type: SubscribeType, observer: Callback&lt;Array&lt;string&gt;&gt;\| Callback&lt;Array&lt;ChangeInfo&gt;&gt;): void
Registers the data change event listener for the RDB store. When the data in the RDB store changes, a callback is invoked to return the data changes.
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core **System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
...@@ -3167,7 +3359,7 @@ Registers an observer for this RDB store. When the data in the RDB store changes ...@@ -3167,7 +3359,7 @@ Registers an observer for this RDB store. When the data in the RDB store changes
| -------- | ----------------------------------- | ---- | ------------------------------------------- | | -------- | ----------------------------------- | ---- | ------------------------------------------- |
| event | string | Yes | Event to observe. The value is **dataChange**, which indicates a data change event. | | event | string | Yes | Event to observe. The value is **dataChange**, which indicates a data change event. |
| type | [SubscribeType](#subscribetype) | Yes | Subscription type to register.| | type | [SubscribeType](#subscribetype) | Yes | Subscription type to register.|
| observer | Callback&lt;Array&lt;string&gt;&gt; | Yes | Callback invoked to return the data change event. **Array<string>** indicates the IDs of the peer devices whose data in the database is changed.| | observer | Callback&lt;Array&lt;string&gt;&gt; \| Callback&lt;Array&lt;[ChangeInfo](#changeinfo10)&gt;&gt; | Yes | Callback invoked to return the data change event.<br>If **type** is **SUBSCRIBE_TYPE_REMOTE**, **observer** must be **Callback&lt;Array&lt;string&gt;&gt;**, where **Array&lt;string&gt;** specifies the IDs of the peer devices with data changes.<br> If **type** is **SUBSCRIBE_TYPE_CLOUD**, **observer** must be **Callback&lt;Array&lt;string&gt;&gt;**, where **Array&lt;string&gt;** specifies the cloud accounts with data changes.<br> If **type** is **SUBSCRIBE_TYPE_CLOUD_DETAILS**, **observer** must be **Callback&lt;Array&lt;ChangeInfo&gt;&gt;**, where **Array&lt;ChangeInfo&gt;** specifies the details about the device-cloud synchronization.|
**Example** **Example**
...@@ -3188,20 +3380,51 @@ try { ...@@ -3188,20 +3380,51 @@ try {
off(event:'dataChange', type: SubscribeType, observer: Callback&lt;Array&lt;string&gt;&gt;): void off(event:'dataChange', type: SubscribeType, observer: Callback&lt;Array&lt;string&gt;&gt;): void
Unregisters the observer of the specified type from the RDB store. This API uses a callback to return the result. Unregisters the data change event listener.
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core **System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
**Parameters** **Parameters**
| Name | Type | Mandatory| Description | | Name | Type | Mandatory| Description |
| -------- | ---------------------------------- | ---- | ------------------------------------------ | | -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ |
| event | string | Yes | Event type. The value is **dataChange**, which indicates a data change event. | | event | string | Yes | Event type. The value is **dataChange**, which indicates a data change event. |
| type | [SubscribeType](#subscribetype) | Yes | Subscription type to unregister. | | type | [SubscribeType](#subscribetype)| Yes | Subscription type to unregister. |
| observer | Callback&lt;Array&lt;string&gt;&gt; | Yes | Callback for the data change event. **Array<string>** indicates the IDs of the peer devices whose data in the database is changed.| | observer | Callback&lt;Array&lt;string&gt;&gt; | Yes | Callback for the data change event. **Array<string>** indicates the IDs of the peer devices whose data in the database is changed.|
**Example** **Example**
```
function storeObserver(devices) {
for (let i = 0; i < devices.length; i++) {
console.info(`device= ${devices[i]} data changed`);
}
}
try {
store.off('dataChange', relationalStore.SubscribeType.SUBSCRIBE_TYPE_REMOTE, storeObserver);
} catch (err) {
console.error(`Unregister observer failed, code is ${err.code},message is ${err.message}`);
}
```
### off('dataChange')<sup>10+</sup>
off(event:'dataChange', type: SubscribeType, observer?: Callback&lt;Array&lt;string&gt;&gt;\| Callback&lt;Array&lt;ChangeInfo&gt;&gt;): void
Unregisters the data change event listener.
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
**Parameters**
| Name | Type | Mandatory| Description |
| -------- | ---------------------------------- | ---- | ------------------------------------------ |
| event | string | Yes | Event to observe. The value is **dataChange**, which indicates a data change event. |
| type | [SubscribeType](#subscribetype) | Yes | Subscription type to register. |
| observer | Callback&lt;Array&lt;string&gt;&gt;\| Callback&lt;Array&lt;[ChangeInfo](#changeinfo10)&gt;&gt; | No| Callback invoked to return the result.<br>If **type** is **SUBSCRIBE_TYPE_REMOTE**, **observer** must be **Callback&lt;Array&lt;string&gt;&gt;**, where **Array&lt;string&gt;** specifies the IDs of the peer devices with data changes.<br> If **type** is **SUBSCRIBE_TYPE_CLOUD**, **observer** must be **Callback&lt;Array&lt;string&gt;&gt;**, where **Array&lt;string&gt;** specifies the cloud accounts with data changes.<br> If **type** is **SUBSCRIBE_TYPE_CLOUD_DETAILS**, **observer** must be **Callback&lt;Array&lt;ChangeInfo&gt;&gt;**, where **Array&lt;ChangeInfo&gt;** specifies the details about the device-cloud synchronization.<br> If **observer** is not specified, listening for all data change events of the specified **type** will be canceled.|
**Example**
```js ```js
function storeObserver(devices) { function storeObserver(devices) {
for (let i = 0; i < devices.length; i++) { for (let i = 0; i < devices.length; i++) {
...@@ -3668,7 +3891,7 @@ Obtains the value of the double type based on the specified column and the curre ...@@ -3668,7 +3891,7 @@ Obtains the value of the double type based on the specified column and the curre
| Type | Description | | Type | Description |
| ------ | ---------------------------- | | ------ | ---------------------------- |
| number | Value obtained.| | number | Returns the value obtained.|
**Error codes** **Error codes**
...@@ -3684,6 +3907,75 @@ For details about the error codes, see [RDB Error Codes](../errorcodes/errorcode ...@@ -3684,6 +3907,75 @@ For details about the error codes, see [RDB Error Codes](../errorcodes/errorcode
const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY")); const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY"));
``` ```
### getAsset<sup>10+</sup>
getAsset(columnIndex: number): Asset
Obtains the value in the [Asset](#asset10) format based on the specified column and current row.
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
**Parameters**
| Name | Type | Mandatory | Description |
| ----------- | ------ | --- | ------------ |
| columnIndex | number | Yes | Index of the target column, starting from 0.|
**Return value**
| Type | Description |
| --------------- | -------------------------- |
| [Asset](#asset10) | Returns the value obtained.|
**Error codes**
For details about the error codes, see [RDB Error Codes](../errorcodes/errorcode-data-rdb.md).
| **ID**| **Error Message** |
| --------- | ------------------------------------------------------------ |
| 14800013 | The column value is null or the column type is incompatible. |
**Example**
```js
const doc = resultSet.getAsset(resultSet.getColumnIndex("DOC"));
```
### getAssets<sup>10+</sup>
getAssets(columnIndex: number): Assets
Obtains the value in the [Assets](#assets10) format based on the specified column and current row.
**System capability**: SystemCapability.DistributedDataManager.RelationalStore.Core
**Parameters**
| Name | Type | Mandatory | Description |
| ----------- | ------ | --- | ------------ |
| columnIndex | number | Yes | Index of the target column, starting from 0.|
**Return value**
| Type | Description |
| ---------------- | ---------------------------- |
| [Assets](#assets10)| Returns the value obtained.|
**Error codes**
For details about the error codes, see [RDB Error Codes](../errorcodes/errorcode-data-rdb.md).
| **ID**| **Error Message** |
| ------------ | ------------------------------------------------------------ |
| 14800013 | The column value is null or the column type is incompatible. |
**Example**
```js
const docs = resultSet.getAssets(resultSet.getColumnIndex("DOCS"));
```
### isColumnNull ### isColumnNull
isColumnNull(columnIndex: number): boolean isColumnNull(columnIndex: number): boolean
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册