1/* 2 * Copyright (c) 2025 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// [Start complex_class_instance_object_using_sendable] 17import { relationalStore, ValuesBucket } from '@kit.ArkData'; 18import { collections, taskpool } from '@kit.ArkTS'; 19import { IValueBucket, SharedValuesBucket } from './SharedValuesBucket'; 20import { Material } from './Material'; 21 22@Concurrent 23async function create(context: Context) { 24 const CONFIG: relationalStore.StoreConfig = { 25 name: 'Store.db', 26 securityLevel: relationalStore.SecurityLevel.S1, 27 }; 28 29 // 默认数据库文件路径为 context.databaseDir + rdb + StoreConfig.name 30 let store: relationalStore.RdbStore = await relationalStore.getRdbStore(context, CONFIG); 31 console.info(`Create Store.db successfully!`); 32 33 // 创建表 34 const CREATE_TABLE_SQL = 'CREATE TABLE IF NOT EXISTS test (' + 35 'id INTEGER PRIMARY KEY AUTOINCREMENT, ' + 36 'name TEXT NOT NULL, ' + 37 'age INTEGER, ' + 38 'salary REAL, ' + 39 'blobType BLOB)'; 40 await store.executeSql(CREATE_TABLE_SQL); 41 console.info(`Create table test successfully!`); 42} 43 44@Concurrent 45async function insert(context: Context, valueBucketArray: collections.Array<SharedValuesBucket | undefined>) { 46 const CONFIG: relationalStore.StoreConfig = { 47 name: 'Store.db', 48 securityLevel: relationalStore.SecurityLevel.S1, 49 }; 50 51 // 默认数据库文件路径为 context.databaseDir + rdb + StoreConfig.name 52 let store: relationalStore.RdbStore = await relationalStore.getRdbStore(context, CONFIG); 53 console.info(`Create Store.db successfully!`); 54 55 // 数据插入 56 await store.batchInsert('test', valueBucketArray as Object as Array<ValuesBucket>); 57} 58 59@Concurrent 60async function query(context: Context): Promise<collections.Array<SharedValuesBucket | undefined>> { 61 const CONFIG: relationalStore.StoreConfig = { 62 name: 'Store.db', 63 securityLevel: relationalStore.SecurityLevel.S1, 64 }; 65 66 // 默认数据库文件路径为 context.databaseDir + rdb + StoreConfig.name 67 let store: relationalStore.RdbStore = await relationalStore.getRdbStore(context, CONFIG); 68 console.info(`Create Store.db successfully!`); 69 70 // 获取结果集 71 let predicates: relationalStore.RdbPredicates = new relationalStore.RdbPredicates('test'); 72 // 查询所有数据 73 let resultSet = await store.query(predicates); 74 console.info(`Query data successfully! row count:${resultSet.rowCount}`); 75 let index = 0; 76 let result = collections.Array.create<SharedValuesBucket | undefined>(resultSet.rowCount, undefined) 77 resultSet.goToFirstRow() 78 do { 79 let v: IValueBucket = { 80 id: resultSet.getLong(resultSet.getColumnIndex('id')), 81 name: resultSet.getString(resultSet.getColumnIndex('name')), 82 age: resultSet.getLong(resultSet.getColumnIndex('age')), 83 salary: resultSet.getLong(resultSet.getColumnIndex('salary')) 84 }; 85 result[index++] = new SharedValuesBucket(v) 86 } while (resultSet.goToNextRow()); 87 resultSet.close(); 88 return result 89} 90 91@Concurrent 92async function clear(context: Context) { 93 const CONFIG: relationalStore.StoreConfig = { 94 name: 'Store.db', 95 securityLevel: relationalStore.SecurityLevel.S1, 96 }; 97 98 // 默认数据库文件路径为 context.databaseDir + rdb + StoreConfig.name 99 await relationalStore.deleteRdbStore(context, CONFIG); 100 console.info(`Delete Store.db successfully!`); 101} 102 103function initMaterial(): Material { 104 // 数据准备 105 const count = 5 106 let valueBucketArray = collections.Array.create<SharedValuesBucket | undefined>(count, undefined); 107 for (let i = 0; i < count; i++) { 108 let v: IValueBucket = { 109 id: i, 110 name: 'zhangsan' + i, 111 age: 20, 112 salary: 5000 + 50 * i 113 }; 114 valueBucketArray[i] = new SharedValuesBucket(v); 115 } 116 let material = new Material(1, 'test', valueBucketArray); 117 return material; 118} 119 120@Entry 121@Component 122struct Index { 123 @State message: string = 'Hello World'; 124 125 build() { 126 RelativeContainer() { 127 Text(this.message) 128 .id('HelloWorld') 129 .fontSize(50) 130 .fontWeight(FontWeight.Bold) 131 .alignRules({ 132 center: { anchor: '__container__', align: VerticalAlign.Center }, 133 middle: { anchor: '__container__', align: HorizontalAlign.Center } 134 }) 135 .onClick(async () => { 136 let context: Context = this.getUIContext().getHostContext() as Context; 137 let material = initMaterial(); 138 await taskpool.execute(create, context); 139 await taskpool.execute(insert, context, material.getBuckets()); 140 let index = 0; 141 let ret: collections.Array<SharedValuesBucket> = 142 await taskpool.execute(query, context) as collections.Array<SharedValuesBucket>; 143 material.setBuckets(ret); 144 for (let v of ret.values()) { 145 console.info(`Row[${index}].id = ${v.id}`); 146 console.info(`Row[${index}].name = ${v.name}`); 147 console.info(`Row[${index}].age = ${v.age}`); 148 console.info(`Row[${index}].salary = ${v.salary}`); 149 index++; 150 } 151 await taskpool.execute(clear, context); 152 this.message = 'success'; 153 }) 154 } 155 .height('100%') 156 .width('100%') 157 } 158} 159 160// [End complex_class_instance_object_using_sendable]