diff --git a/data/eTSDDMQuery/README_zh.md b/data/eTSDDMQuery/README_zh.md new file mode 100644 index 0000000000000000000000000000000000000000..e7930762c50296d813c80f29f5eb6a5739baacd8 --- /dev/null +++ b/data/eTSDDMQuery/README_zh.md @@ -0,0 +1,23 @@ +# DDM结果集与谓词查询 + +### 简介 + +本示例展示了分布式数据管理中,如何通过构建query对象, 查询kvstore中的数据,获取结果集。 + +### 使用说明 + +1.点击**添加数据**,将模板数据存入数据库 + +2.点击**查询指定字段结果集**,从数据库中查询key包含指定字段的结果集 + +3.点击**查询指定值条目**,通过谓词从数据库中查询key包含指定字段的结果集 + +4.点击**与条件查询**,通过谓词从数据库中查询指定FieldNode对应的值不为特定值的结果集 + +5.点击**查询結果降序**,通过谓词从数据库中查询指定FieldNode对应的值不为特定值且降序排序的结果集 + +6.点击**查询小于指定值**,通过谓词从数据库中查询指定FieldNode对应的值小于特定值的键值对列表 + +### 约束与限制 + +本示例仅支持在标准系统上运行。 \ No newline at end of file diff --git a/data/eTSDDMQuery/build.gradle b/data/eTSDDMQuery/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..f1b1a556a0c09953268d32e7da54b1e1ba664bac --- /dev/null +++ b/data/eTSDDMQuery/build.gradle @@ -0,0 +1,34 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. +apply plugin: 'com.huawei.ohos.app' + +//For instructions on signature configuration, see https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ide_debug_device-0000001053822404#section1112183053510 +ohos { + compileSdkVersion 8 + supportSystem "standard" +} + +buildscript { + repositories { + maven { + url 'https://repo.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } + } + dependencies { + classpath 'com.huawei.ohos:hap:3.0.5.2' + classpath 'com.huawei.ohos:decctest:1.2.7.2' + } +} + +allprojects { + repositories { + maven { + url 'https://repo.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } + } +} diff --git a/data/eTSDDMQuery/entry/build.gradle b/data/eTSDDMQuery/entry/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..cef7b37de8f320938458ca42edde1462669836d2 --- /dev/null +++ b/data/eTSDDMQuery/entry/build.gradle @@ -0,0 +1,21 @@ +apply plugin: 'com.huawei.ohos.hap' +//For instructions on signature configuration, see https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ide_debug_device-0000001053822404#section1112183053510 +ohos { + compileSdkVersion 8 + defaultConfig { + compatibleSdkVersion 7 + } + buildTypes { + release { + proguardOpt { + proguardEnabled false + rulesFiles 'proguard-rules.pro' + } + } + } +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar', '*.har']) + testImplementation 'junit:junit:4.13.1' +} diff --git a/data/eTSDDMQuery/entry/src/main/config.json b/data/eTSDDMQuery/entry/src/main/config.json new file mode 100644 index 0000000000000000000000000000000000000000..20a2ab4d296823532f2e422d86b037e84fe66b38 --- /dev/null +++ b/data/eTSDDMQuery/entry/src/main/config.json @@ -0,0 +1,71 @@ +{ + "app": { + "bundleName": "ohos.samples.etsddmquery", + "vendor": "samples", + "version": { + "code": 1000000, + "name": "1.0.0" + } + }, + "deviceConfig": {}, + "module": { + "package": "ohos.samples.etsddmquery", + "name": ".MyApplication", + "mainAbility": ".MainAbility", + "deviceType": [ + "phone" + ], + "distro": { + "deliveryWithInstall": true, + "moduleName": "entry", + "moduleType": "entry", + "installationFree": false + }, + "abilities": [ + { + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ], + "orientation": "unspecified", + "visible": true, + "srcPath": "MainAbility", + "name": ".MainAbility", + "srcLanguage": "ets", + "icon": "$media:icon", + "description": "$string:description_mainability", + "formsEnabled": false, + "label": "$string:entry_MainAbility", + "type": "page", + "launchType": "standard" + } + ], + "js": [ + { + "mode": { + "syntax": "ets", + "type": "pageAbility" + }, + "pages": [ + "pages/index" + ], + "name": ".MainAbility", + "window": { + "designWidth": 720, + "autoDesignWidth": false + } + } + ], + "reqPermissions": [ + { + "name": "ohos.permission.DISTRIBUTED_DATASYNC" + } + ] + } +} \ No newline at end of file diff --git a/data/eTSDDMQuery/entry/src/main/ets/MainAbility/app.ets b/data/eTSDDMQuery/entry/src/main/ets/MainAbility/app.ets new file mode 100644 index 0000000000000000000000000000000000000000..58946d00c628e0bba577551f68471b8dae55f5f8 --- /dev/null +++ b/data/eTSDDMQuery/entry/src/main/ets/MainAbility/app.ets @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export default { + onCreate() { + console.info('Application onCreate') + }, + onDestroy() { + console.info('Application onDestroy') + }, +} \ No newline at end of file diff --git a/data/eTSDDMQuery/entry/src/main/ets/MainAbility/common/query.ets b/data/eTSDDMQuery/entry/src/main/ets/MainAbility/common/query.ets new file mode 100644 index 0000000000000000000000000000000000000000..698e06940b1534741ff88ffc278ed83035201585 --- /dev/null +++ b/data/eTSDDMQuery/entry/src/main/ets/MainAbility/common/query.ets @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { KvStoreModule } from "../model/kvStoreModule.ets" +import distributedData from '@ohos.data.distributedData' + +let kvStoreModule: KvStoreModule = new KvStoreModule('ohos.samples.etsddmquery', 'kvStoreTest') +let query = new distributedData.Query() + +@Component +export struct Query { + private options: Resource[] = [$r('app.string.push'), $r('app.string.resultSet'), $r('app.string.prefixKey'), + $r('app.string.orderByDesc'), $r('app.string.addResult'), $r('app.string.lessThan')] + @State textTitle: string = '' + private queryTest = null + + onQuery(data) { + console.log('[KvStoreModule] onQuery') + this.textTitle = data + } + + aboutToAppear() { + kvStoreModule.createKvStore() + this.queryTest = this.onQuery.bind(this) + } + + build() { + Scroll() { + Column() { + Scroll() { + Text(this.textTitle) + .fontSize(25) + .width('100%') + .padding(10) + .constraintSize({ minHeight: 150 }) + .layoutWeight(1) + } + .width('90%') + .height('30%') + .border({ width: 1, color: '#000000', radius: 10, style: 0 }) + + ForEach(this.options, (item, index) => { + Button() { + Text(item) + .width('80%') + .height(30) + .fontSize(20) + .textAlign(TextAlign.Center) + .onClick(() => { + console.log(`[KvStoreModule] onClick index= ${index}`) + switch (index) { + case 0: + let key = 'batch_test_string_key' + kvStoreModule.put(key, this.queryTest) + break + case 1: + kvStoreModule.getResultSet('batch_test', this.queryTest) + break + case 2: + query.prefixKey('batch_test_string') + kvStoreModule.getResultSetQuery(query, this.queryTest) + break + case 3: + query.notEqualTo('$.name', 3) + query.orderByDesc('$.name') + kvStoreModule.getResultSetQuery(query, this.queryTest) + break + case 4: + query.notEqualTo('$.name', 5) + query.and() + query.notEqualTo('$.name', 3) + kvStoreModule.getResultSetQuery(query, this.queryTest) + break + case 5: + query.lessThan('$.name', 7) + kvStoreModule.getEntries(query, this.queryTest) + break + default: + break + } + }) + } + .margin({ top: 20 }) + .backgroundColor('#0D9FFB') + }, item => JSON.stringify(item)) + } + .margin({ top: 20 }) + .width('100%') + } + .layoutWeight(1) + } +} \ No newline at end of file diff --git a/data/eTSDDMQuery/entry/src/main/ets/MainAbility/common/titleBar.ets b/data/eTSDDMQuery/entry/src/main/ets/MainAbility/common/titleBar.ets new file mode 100644 index 0000000000000000000000000000000000000000..3566c7486f04b0bee0269878e26d40bfd5554c35 --- /dev/null +++ b/data/eTSDDMQuery/entry/src/main/ets/MainAbility/common/titleBar.ets @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +@Component +export struct TitleBar { + build() { + Row() { + Text($r('app.string.entry_MainAbility')) + .fontColor(Color.White) + .fontSize(30) + .layoutWeight(1) + } + .width('100%') + .height('8%') + .constraintSize({ minHeight: 50 }) + .padding({ left: 15 }) + .backgroundColor('#0D9FFB') + } +} \ No newline at end of file diff --git a/data/eTSDDMQuery/entry/src/main/ets/MainAbility/model/kvStoreModule.ets b/data/eTSDDMQuery/entry/src/main/ets/MainAbility/model/kvStoreModule.ets new file mode 100644 index 0000000000000000000000000000000000000000..335e29781466d940ed37783169e9146193a6a1d0 --- /dev/null +++ b/data/eTSDDMQuery/entry/src/main/ets/MainAbility/model/kvStoreModule.ets @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import distributedData from '@ohos.data.distributedData' + +const TAG = '[KvStoreModule]:' + +export class KvStoreModule { + private bundleName: string = '' + private storeId: string = '' + SingleKVStore + + constructor(bundleNme: string, storeId: string) { + this.bundleName = bundleNme + this.storeId = storeId + } + + createKvStore() { + let config = { + bundleName: this.bundleName, + userInfo: { + userId: '0', + userType: 0 + } + } + let options = { + createIfMissing: true, + encrypt: false, + backup: false, + autoSync: true, + kvStoreType: 1, + securityLevel: 3, + schema: '' + } + distributedData.createKVManager(config).then((manager) => { + console.info(`${TAG} createKVManager success, kvManager = ${JSON.stringify(manager)}`) + + let name = new distributedData.FieldNode('name') + name.type = 1 + name.nullable = false + name.default = 0 + + let schema = new distributedData.Schema() + schema.root.appendChild(name) + schema.indexes = ['$.name'] + schema.mode = 1 + options.schema = schema.toJsonString() + console.log(`${TAG} ${schema.toJsonString()}`) + manager.getKVStore(this.storeId, options).then((store) => { + console.info(`${TAG} getKVStore success, SingleKVStore = ${store}`) + this.SingleKVStore = store + }) + }) + } + + put(key, callback) { + for (let i = 0; i < 10; i++) { + let value = '{"name":' + i + '}' + console.log(`${TAG} this.SingleKVStore = ${this.SingleKVStore} ${key} ${i} ${value}`) + this.SingleKVStore.put(key + i, value).then((data) => { + console.info(`${TAG} SingleKVStore.put success`) + callback('put success') + }).catch((err) => { + console.error(`${TAG} SingleKVStore.put failed ${JSON.stringify(err)}`) + }) + } + } + + getResultSet(key, callback) { + this.SingleKVStore.getResultSet(key, function (err, result){ + this.resultSet(result, callback) + }.bind(this)) + } + + resultSet(result, callback) { + console.log(`${TAG} getResultSet success result.getCount = ${result.getCount()}`) + console.log(`${TAG} getResultSet success result.moveToFirst = ${result.moveToFirst()}`) + console.log(`${TAG} getResultSet success result.entry = ${JSON.stringify(result.getEntry())}`) + let entries = '' + for (let i = 0; i < result.getCount(); i++) { + console.info(`${TAG} key = ${JSON.stringify(result.getEntry().key)}`) + console.info(`${TAG} value = ${JSON.stringify(result.getEntry().value.value)}`) + entries += `key:${JSON.stringify(result.getEntry().key)},value:${JSON.stringify(result.getEntry().value.value)}\n` + console.info(`${TAG} moveToNext = ${result.moveToNext()}`) + } + callback(entries) + this.SingleKVStore.closeResultSet(result, function (err, data) { + console.info(`${TAG} getResultSet closeResultSet success`) + }) + } + + getResultSetQuery(query, callback) { + console.log(`${TAG} getResultSetQuery enter`) + this.SingleKVStore.getResultSet(query, function (err, result){ + query = null + console.log(`${TAG} getResultSetQuery getResultSet,err=${err}`) + this.resultSet(result, callback) + }.bind(this)) + } + + getEntries(query, callback) { + try { + this.SingleKVStore.getEntries(query, function (entries){ + console.log(`${TAG} getEntries success ${JSON.stringify(entries)}`) + query = null + this.resultSet(entries, callback) + }.bind(this)) + } catch (err) { + console.info(`${TAG} getEntries fail ${JSON.stringify(err)}`) + } + } +} \ No newline at end of file diff --git a/data/eTSDDMQuery/entry/src/main/ets/MainAbility/pages/index.ets b/data/eTSDDMQuery/entry/src/main/ets/MainAbility/pages/index.ets new file mode 100644 index 0000000000000000000000000000000000000000..86ecd7dd652d320a2e3617997b8412e77e266d3e --- /dev/null +++ b/data/eTSDDMQuery/entry/src/main/ets/MainAbility/pages/index.ets @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2020 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { Query } from "../common/query.ets" +import { TitleBar } from "../common/titleBar.ets" + +@Entry +@Component +struct Index { + + build() { + Column(){ + TitleBar() + Query() + } + .width('100%') + .height('100%') + } +} \ No newline at end of file diff --git a/data/eTSDDMQuery/entry/src/main/resources/base/element/string.json b/data/eTSDDMQuery/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..314c8ff505ecca6b4faf70ccaf15c74e30072360 --- /dev/null +++ b/data/eTSDDMQuery/entry/src/main/resources/base/element/string.json @@ -0,0 +1,36 @@ +{ + "string": [ + { + "name": "entry_MainAbility", + "value": "eTSDDMQuery" + }, + { + "name": "description_mainability", + "value": "eTS_Empty Ability" + }, + { + "name": "push", + "value": "Add Data" + }, + { + "name": "resultSet", + "value": "Query the result set" + }, + { + "name": "prefixKey", + "value": "Query the specified value entry" + }, + { + "name": "addResult", + "value": "And conditional query" + }, + { + "name": "orderByDesc", + "value": "Query results descending order" + }, + { + "name": "lessThan", + "value": "Query smaller than value" + } + ] +} \ No newline at end of file diff --git a/data/eTSDDMQuery/entry/src/main/resources/base/media/icon.png b/data/eTSDDMQuery/entry/src/main/resources/base/media/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..ce307a8827bd75456441ceb57d530e4c8d45d36c Binary files /dev/null and b/data/eTSDDMQuery/entry/src/main/resources/base/media/icon.png differ diff --git a/data/eTSDDMQuery/entry/src/main/resources/en/element/string.json b/data/eTSDDMQuery/entry/src/main/resources/en/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..284337c15fd8310e1ea8beb1f8ee25e847d35389 --- /dev/null +++ b/data/eTSDDMQuery/entry/src/main/resources/en/element/string.json @@ -0,0 +1,36 @@ +{ + "string": [ + { + "name": "entry_MainAbility", + "value": "eTSDDMQuery" + }, + { + "name": "description_mainability", + "value": "eTSKvStore Ability" + }, + { + "name": "push", + "value": "Add Data" + }, + { + "name": "resultSet", + "value": "Query the result set" + }, + { + "name": "prefixKey", + "value": "Query the specified value entry" + }, + { + "name": "addResult", + "value": "And conditional query" + }, + { + "name": "orderByDesc", + "value": "Query results descending order" + }, + { + "name": "lessThan", + "value": "Query smaller than value" + } + ] +} \ No newline at end of file diff --git a/data/eTSDDMQuery/entry/src/main/resources/zh/element/string.json b/data/eTSDDMQuery/entry/src/main/resources/zh/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..3050c8ff259c873371a58b7463f2ddd918e8087d --- /dev/null +++ b/data/eTSDDMQuery/entry/src/main/resources/zh/element/string.json @@ -0,0 +1,36 @@ +{ + "string": [ + { + "name": "entry_MainAbility", + "value": "eTSDDMQuery" + }, + { + "name": "description_mainability", + "value": "eTSKvStore Ability" + }, + { + "name": "push", + "value": "添加数据" + }, + { + "name": "resultSet", + "value": "查询指定字段结果集" + }, + { + "name": "prefixKey", + "value": "查询指定值条目" + }, + { + "name": "addResult", + "value": "与条件查询" + }, + { + "name": "orderByDesc", + "value": "查询結果降序" + }, + { + "name": "lessThan", + "value": "查询小于指定值" + } + ] +} \ No newline at end of file diff --git a/data/eTSDDMQuery/screenshots/devices/eTSDDMQuery.png b/data/eTSDDMQuery/screenshots/devices/eTSDDMQuery.png new file mode 100644 index 0000000000000000000000000000000000000000..9f4ff7df6923585dface07927f9a8c28464021e0 Binary files /dev/null and b/data/eTSDDMQuery/screenshots/devices/eTSDDMQuery.png differ diff --git a/data/eTSDDMQuery/settings.gradle b/data/eTSDDMQuery/settings.gradle new file mode 100644 index 0000000000000000000000000000000000000000..4773db73233a570c2d0c01a22e75321acfbf7a07 --- /dev/null +++ b/data/eTSDDMQuery/settings.gradle @@ -0,0 +1 @@ +include ':entry'