1/* 2 * Copyright (c) 2022 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 DataShareExtensionAbility from '@ohos.application.DataShareExtensionAbility' 17import rdb from '@ohos.data.rdb' 18import Logger from '../util/Logger' 19 20const TAG: string = 'DataShareExtAbility' 21const TABLE_NAME: string = 'books' 22const STORE_CONFIG: rdb.StoreConfig = { name: 'books.db' } 23const SQL_CREATE_TABLE: string = 'CREATE TABLE IF NOT EXISTS books (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, introduction TEXT NOT NULL)' 24let rdbStore: rdb.RdbStore = undefined 25 26// 对应FA的DataAbility下的Data.ts 27export default class DataShareExtAbility extends DataShareExtensionAbility { 28 29 // 对应FA的onInitialized 30 onCreate(want, callback) { 31 Logger.info(TAG, ` DataShareExtAbility onCreate, want: ${JSON.stringify(want.abilityName)}`) 32 rdb.getRdbStore(this.context, STORE_CONFIG, 1, (err, data) => { 33 Logger.info(TAG, `DataShareExtAbility getRdbStore done`) 34 rdbStore = data 35 rdbStore.executeSql(SQL_CREATE_TABLE, [], () => { 36 Logger.info(TAG, `DataShareExtAbility executeSql done`) 37 }) 38 if (callback) { 39 callback() 40 } 41 }) 42 Logger.info(TAG, `DataShareExtAbility onCreate end`) 43 } 44 45 // 对应FA的insert 46 insert(uri, value, callback) { 47 Logger.info(TAG, `[insert] enter`) 48 if (value === null) { 49 Logger.info(' [insert] invalid valueBuckets') 50 return 51 } 52 Logger.info(TAG, ` [insert] value = ${JSON.stringify(value)}`) 53 if (rdbStore) { 54 rdbStore.insert(TABLE_NAME, value, (err, ret) => { 55 Logger.info(TAG, ` [insert] leave ${ret}`) 56 if (callback !== undefined) { 57 callback(err, ret) 58 } 59 }) 60 } 61 } 62 63 // 对应FA的delete 64 delete(uri, predicates, callback) { 65 Logger.info(TAG, `delete`) 66 try { 67 if (rdbStore) { 68 rdbStore.delete(TABLE_NAME, predicates, (error, ret) => { 69 Logger.info(TAG, `delete ret: ${ret}`) 70 callback(error, ret) 71 }) 72 } 73 } catch (error) { 74 Logger.error(TAG, `delete error: ${JSON.stringify(error)}`) 75 } 76 } 77 78 // 对应FA的query 79 query(uri, predicates, columns, callback) { 80 Logger.info(TAG, `query enter`) 81 try { 82 if (rdbStore) { 83 rdbStore.query(TABLE_NAME, predicates, columns, (err, resultSet) => { 84 Logger.info(TAG, `query ret: ${resultSet}`) 85 if (resultSet !== undefined) { 86 Logger.info(TAG, `query resultSet.rowCount: ${JSON.stringify(resultSet.rowCount)}`) 87 } 88 if (callback !== undefined) { 89 callback(err, resultSet) 90 } 91 }) 92 } 93 } catch (err) { 94 Logger.error(TAG, `query error: ${JSON.stringify(err)}`) 95 } 96 Logger.info(TAG, `query leave`) 97 } 98 99 // 对应FA的update 100 update(uri, predicates, value, callback) { 101 if (predicates === null || predicates === undefined) { 102 return 103 } 104 if (rdbStore) { 105 rdbStore.update(TABLE_NAME, value, predicates, function (err, ret) { 106 if (callback !== undefined) { 107 callback(err, ret) 108 } 109 }) 110 } 111 } 112 113 // 服务端使用的URI转换为用户传入的初始URI时服务端回调此接口,该方法可以选择性重写。 114 denormalizeUri(uri, callback) { 115 Logger.error(TAG, `denormalizeUri uri: ${JSON.stringify(uri)}`) 116 } 117 118 // 用户给定的URI转换为服务端使用的URI时回调此接口,该方法可以选择性重写。 119 normalizeUri(uri, callback) { 120 Logger.error(TAG, `denormalizeUri uri: ${JSON.stringify(uri)}`) 121 } 122}