1/* 2* Copyright (c) 2023 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*/ 15import featureAbility from '@ohos.ability.featureAbility'; 16 17import data_rdb from '@ohos.data.relationalStore'; 18import deviceManager from '@ohos.distributedHardware.deviceManager'; 19import TestApi from '../../../../../../../../../../../testtools/disjsTest/server/testApi.js'; 20 21let context = featureAbility.getContext(); 22globalThis.abilityContext = context; 23 24const CREATE_TABLE_TEST = "CREATE TABLE IF NOT EXISTS test (" + "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "name TEXT NOT NULL, " + "age INTEGER, " + "salary REAL, " + "blobType BLOB)"; 25 26var rdbStore = undefined; 27const STORE_CONFIG = { 28 name: "RemoteRdb.db", 29 securityLevel: data_rdb.SecurityLevel.S1 30}; 31 32export default { 33 34 data: { 35 title: "" 36 }, 37 38 onInit() { 39 this.createRDBStore(); 40 }, 41 onShow(){ 42 43 }, 44 45 getIndexContext(){ 46 console.log("rdbStore " + rdbStore); 47 return rdbStore; 48 49 }, 50 51 async initDeviceMonitor() { 52 console.log("create device manager before"); 53 try { 54 await deviceManager.createDeviceManager('com.ohos.distributerdbdisjs', (err, manager) => { 55 if (err) { 56 console.log("create device manager failed, err=" + err); 57 return; 58 } 59 let dmInstance = manager; 60 console.log("create device manager success dmInstance: "+ JSON.stringify(dmInstance)); 61 dmInstance.on("deviceStateChange", (data) => { 62 console.log("deviceStateChange: " + JSON.stringify(data)); 63 }); 64 }); 65 } catch (error) { 66 console.log("createDeviceManager error : " + error.message); 67 } 68 69 }, 70 async createRDBStore(){ 71 console.log("first context " + context); 72 let promise = data_rdb.getRdbStore(context, STORE_CONFIG); 73 promise.then(async (back) => { 74 rdbStore = back; 75 console.log("Get RdbStore successfully rdbStore " + rdbStore); 76 }).catch((err) => { 77 console.log("Get RdbStore failed, err: " + err); 78 }) 79 await promise; 80 await rdbStore.executeSql(CREATE_TABLE_TEST, null); 81 let testApi = new TestApi(); 82 this.setDistributedTables(); 83 }, 84 85 setDistributedTables(){ 86 console.info("setDistributedTables before"); 87 let back = rdbStore.setDistributedTables(["test"]); 88 back.then(() => { 89 console.info("SetDistributedTables successfully."); 90 this.insertData(); 91 }).catch((err) => { 92 console.info("SetDistributedTables failed, err: " + err.code); 93 }) 94 }, 95 96 async insertData(){ 97 const valueBucket = { 98 "name": "Lisa", 99 "age": 18, 100 "salary": 100.5, 101 "blobType": new Uint8Array([1, 2, 3, 4, 5]) 102 } 103 console.log("Insert insertData start."); 104 let promise = rdbStore.insert("test", valueBucket); 105 promise.then((rowId) => { 106 console.log("Insert is successful, rowId = " + rowId); 107 }).catch((err) => { 108 console.log("Insert is failed err: "+err.code+" "+err.message); 109 }) 110 await promise; 111 } 112} 113