1/* 2 * Copyright (c) 2022-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 rdb from '@ohos.data.relationalStore' 17import DataShareExtensionAbility from '@ohos.application.DataShareExtensionAbility' 18import { logger, SQL_CREATE_TABLE, STORE_CONFIG, TABLE_NAME } from '@ohos/common' 19 20const TAG: string = 'DataShareAbility' 21let rdbStore: rdb.RdbStore = undefined 22 23export default class DataShareAbility extends DataShareExtensionAbility { 24 onCreate(want, callback) { 25 logger.info(TAG, 'onCreate') 26 try { 27 rdb.getRdbStore(globalThis.context, STORE_CONFIG, (error, data) => { 28 logger.info(TAG, 'DataShareExtAbility getRdbStore done') 29 rdbStore = data 30 rdbStore.executeSql(SQL_CREATE_TABLE, [], () => { 31 logger.info(TAG, 'DataShareExtAbility executeSql done') 32 if (callback) { 33 callback() 34 } 35 }) 36 }) 37 } catch (error) { 38 logger.error(TAG, `getRdbStore error: ${JSON.stringify(error)}`) 39 } 40 } 41 42 insert(uri: string, valueBucket, callback) { 43 logger.info(TAG, `[insert] enter`) 44 if (valueBucket === null || valueBucket === undefined) { 45 logger.info('[insert] invalid valueBuckets') 46 return 47 } 48 logger.info(TAG, `[insert] value = ${JSON.stringify(valueBucket)}`) 49 if (rdbStore) { 50 try { 51 rdbStore.insert(TABLE_NAME, valueBucket, (error, ret) => { 52 if (callback !== undefined) { 53 callback(error, ret) 54 } 55 }) 56 } catch (error) { 57 logger.error(TAG, `insert error: ${JSON.stringify(error)}`) 58 } 59 } 60 } 61 62 delete(uri, predicates, callback) { 63 logger.info(TAG, `delete`) 64 if (rdbStore) { 65 try { 66 rdbStore.delete(TABLE_NAME, predicates, (error, ret) => { 67 logger.info(TAG, `delete ret: ${ret}`) 68 callback(error, ret) 69 }) 70 } catch (error) { 71 logger.error(TAG, `delete error: ${JSON.stringify(error)}`) 72 } 73 } 74 } 75 76 query(uri, predicates, columns, callback) { 77 logger.info(TAG, `query enter`) 78 if (rdbStore) { 79 try { 80 rdbStore.query(TABLE_NAME, predicates, columns, (error, resultSet) => { 81 logger.info(TAG, `query ret: ${resultSet}`) 82 if (resultSet !== undefined) { 83 logger.info(TAG, `query resultSet.rowCount: ${JSON.stringify(resultSet.rowCount)}`) 84 } 85 if (callback !== undefined) { 86 callback(error, resultSet) 87 } 88 }) 89 } catch (error) { 90 logger.error(TAG, `query error: ${JSON.stringify(error)}`) 91 } 92 } 93 } 94 95 update(uri, predicates, value, callback) { 96 logger.info(TAG, `update enter`) 97 if (predicates === null || predicates === undefined) { 98 return 99 } 100 if (rdbStore) { 101 try { 102 rdbStore.update(TABLE_NAME, value, predicates, (error, ret) => { 103 if (callback !== undefined) { 104 callback(error, ret) 105 } 106 }) 107 } catch (error) { 108 logger.error(TAG, `update error: ${JSON.stringify(error)}`) 109 } 110 } 111 } 112 113 batchInsert(uri, valueBuckets, callback) { 114 logger.info(TAG, `batchInsert enter`) 115 if (rdbStore) { 116 try { 117 rdbStore.batchInsert(TABLE_NAME, valueBuckets, (error, ret) => { 118 if (callback !== undefined) { 119 callback(error, ret) 120 } 121 }) 122 } catch (error) { 123 logger.error(TAG, `update error: ${JSON.stringify(error)}`) 124 } 125 } 126 } 127} 128