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 */ 15 16import featureAbility from '@ohos.ability.featureAbility'; 17import type common from '@ohos.app.ability.common'; 18import type Want from '@ohos.app.ability.Want'; 19import type { AsyncCallback, BusinessError } from '@ohos.base'; 20import dataAbility from '@ohos.data.dataAbility'; 21import rdb from '@ohos.data.rdb'; 22import hilog from '@ohos.hilog'; 23 24let TABLE_NAME = 'book'; 25let STORE_CONFIG: rdb.StoreConfig = { name: 'book.db' }; 26let SQL_CREATE_TABLE = 'CREATE TABLE IF NOT EXISTS book(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, introduction TEXT NOT NULL)'; 27let rdbStore: rdb.RdbStore | undefined = undefined; 28const TAG: string = '[Sample_FAModelAbilityDevelop]'; 29const domain: number = 0xFF00; 30 31class DataAbility { 32 onInitialized(want: Want): void { 33 hilog.info(domain, TAG, 'DataAbility onInitialized, abilityInfo:' + want.bundleName); 34 let context: common.BaseContext = { stageMode: featureAbility.getContext().stageMode }; 35 rdb.getRdbStore(context, STORE_CONFIG, 1, (err, store) => { 36 hilog.info(domain, TAG, 'DataAbility getRdbStore callback'); 37 store.executeSql(SQL_CREATE_TABLE, []); 38 rdbStore = store; 39 }); 40 } 41 42 insert(uri: string, valueBucket: rdb.ValuesBucket, callback: AsyncCallback<number>): void { 43 hilog.info(domain, TAG, 'DataAbility insert start'); 44 if (rdbStore) { 45 rdbStore.insert(TABLE_NAME, valueBucket, callback); 46 } 47 } 48 49 batchInsert(uri: string, valueBuckets: Array<rdb.ValuesBucket>, callback: AsyncCallback<number>): void { 50 hilog.info(domain, TAG, 'DataAbility batch insert start'); 51 if (rdbStore) { 52 for (let i = 0; i < valueBuckets.length; i++) { 53 hilog.info(domain, TAG, 'DataAbility batch insert i=' + i); 54 if (i < valueBuckets.length - 1) { 55 rdbStore.insert(TABLE_NAME, valueBuckets[i], (err: BusinessError, num: number) => { 56 hilog.info(domain, TAG, 'DataAbility batch insert ret=' + num); 57 }); 58 } else { 59 rdbStore.insert(TABLE_NAME, valueBuckets[i], callback); 60 } 61 } 62 } 63 } 64 65 query(uri: string, columns: Array<string>, predicates: dataAbility.DataAbilityPredicates, callback: AsyncCallback<rdb.ResultSet>): void { 66 hilog.info(domain, TAG, 'DataAbility query start'); 67 let rdbPredicates = dataAbility.createRdbPredicates(TABLE_NAME, predicates); 68 if (rdbStore) { 69 rdbStore.query(rdbPredicates, columns, callback); 70 } 71 } 72 73 update(uri: string, valueBucket: rdb.ValuesBucket, predicates: dataAbility.DataAbilityPredicates, callback: AsyncCallback<number>): void { 74 hilog.info(domain, TAG, 'DataAbility update start'); 75 let rdbPredicates = dataAbility.createRdbPredicates(TABLE_NAME, predicates); 76 if (rdbStore) { 77 rdbStore.update(valueBucket, rdbPredicates, callback); 78 } 79 } 80 81 delete(uri: string, predicates: dataAbility.DataAbilityPredicates, callback: AsyncCallback<number>): void { 82 hilog.info(domain, TAG, 'DataAbility delete start'); 83 let rdbPredicates = dataAbility.createRdbPredicates(TABLE_NAME, predicates); 84 if (rdbStore) { 85 rdbStore.delete(rdbPredicates, callback); 86 } 87 } 88} 89 90export default new DataAbility();