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 DataRdb from '@ohos.data.relationalStore'; 17import Account from '../database/Account'; 18import { BusinessError } from '@ohos.base'; 19import CommonConstants from '../constants/CommonConstants'; 20import { GlobalContext } from '../database/GlobalContext'; 21import Logger from '../../model/Logger'; 22 23const TAG: string = `[DatabaseUtils]` 24 25/** 26 * Database operation tool class. 27 */ 28export class DatabaseUtils { 29 constructor(context: Context) { 30 this.createRdbStore(context) 31 } 32 33 /** 34 * Create rdb store 35 * 36 * @param {context} Context 37 * @return {globalThis.rdbStore} return rdbStore RDB database 38 */ 39 async createRdbStore(context: Context) { 40 if (!GlobalContext.getrdbStore()) { 41 // 获取RdbStore 42 await DataRdb.getRdbStore(context, CommonConstants.RDB_STORE_CONFIG) 43 .then((rdbStore: DataRdb.RdbStore) => { 44 if (rdbStore) { 45 // 创建数据表 46 rdbStore.executeSql(CommonConstants.CREATE_TABLE_ACCOUNT).then(() => { 47 Logger.info(TAG, 'executeSql Account successful'); 48 }).catch((error: BusinessError) => { 49 Logger.error(TAG, 'executeSql Account error ' + JSON.stringify(error)); 50 }); 51 GlobalContext.setrdbStore(rdbStore); 52 } 53 }).catch((error: BusinessError) => { 54 Logger.error(TAG, 'createRdbStore error ' + JSON.stringify(error)); 55 }); 56 } 57 return GlobalContext.getrdbStore(); 58 } 59 60 /** 61 * Insert account data. 62 * 63 * @param {Account} account Account entity. 64 * @param {DataRdb.RdbStore} rdbStore RDB database. 65 */ 66 insertAccount(account: Account, rdbStore: DataRdb.RdbStore) { 67 rdbStore.insert(CommonConstants.TABLE_ACCOUNT, account.toValuesBucket()).then((rowId) => { 68 Logger.info(TAG, 'insertAccount successful ' + rowId); 69 }).catch((error: BusinessError) => { 70 Logger.error(TAG, 'insertAccount error ' + JSON.stringify(error)); 71 }); 72 } 73 74 /** 75 * Update account. 76 * 77 * @param {string} accountName Account Name. 78 * @param {Account} account Account entity. 79 * @param {DataRdb.RdbStore} rdbStore RDB database. 80 */ 81 updateAccounts(accountName: string, account: Account, rdbStore: DataRdb.RdbStore) { 82 let predicates: DataRdb.RdbPredicates = new DataRdb.RdbPredicates(CommonConstants.TABLE_ACCOUNT); 83 predicates.equalTo(CommonConstants.FIELD_DISTRIBUTED_ACCOUNT_NAME, accountName); 84 rdbStore.update(account.toValuesBucket(), predicates).then(async (rows) => { 85 Logger.info(`Updated row count: ${rows}`); 86 }).catch((err: BusinessError) => { 87 Logger.error(`Updated failed, code is ${err.code},message is ${err.message}`); 88 }) 89 } 90 91 /** 92 * Delete account data. 93 * 94 * @param {number} localId Account ID. 95 * @param {DataRdb.RdbStore} rdbStore RDB database. 96 */ 97 deleteAccountData(localId: number, rdbStore: DataRdb.RdbStore) { 98 let predicates: DataRdb.RdbPredicates = new DataRdb.RdbPredicates(CommonConstants.TABLE_ACCOUNT); 99 predicates.equalTo(CommonConstants.FIELD_LOCAL_ID, localId); 100 rdbStore.delete(predicates).then(() => { 101 Logger.info(TAG, 'deleteAccountData delete successful '); 102 }).catch((error: BusinessError) => { 103 Logger.error(TAG, 'deleteAccountData delete error ' + JSON.stringify(error)); 104 }); 105 } 106 107 /** 108 * Query account data. 109 * 110 * @param {string} accountName Account Name. 111 * @param {DataRdb.RdbStore} rdbStore RDB database. 112 */ 113 queryAccountData(accountName: string, rdbStore: DataRdb.RdbStore) { 114 return new Promise<DataRdb.ResultSet>((resolve, reject) => { 115 let predicates: DataRdb.RdbPredicates = new DataRdb.RdbPredicates(CommonConstants.TABLE_ACCOUNT); 116 predicates.equalTo(CommonConstants.FIELD_DISTRIBUTED_ACCOUNT_NAME, accountName); 117 rdbStore.query(predicates).then((resultSet) => { 118 resolve(resultSet) 119 Logger.info(TAG, 'queryAccountData query successful'); 120 }).catch((error: BusinessError) => { 121 reject(error) 122 Logger.error(TAG, 'queryAccountData query error ' + JSON.stringify(error)); 123 }); 124 }) 125 } 126} 127export default new DatabaseUtils(GlobalContext.getContext());