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.rdb'; 17 18import { Log } from '../../utils/Log' 19import SettingItemInfo from './SettingItemInfo' 20 21const DB_NAME = 'Camera.db' 22const DB_VERSION = 1 23const TABLE_NAME = 'SETTING' 24const CREATE_TABLE = 'CREATE TABLE IF NOT EXISTS SETTING ' + 25'(id INTEGER PRIMARY KEY AUTOINCREMENT, ' + 26'item_name TEXT, ' + 27'item_value TEXT)' 28 29/** 30 * db manager 31 */ 32export class RdbStoreManager { 33 private TAG = '[RdbStoreManager]:' 34 private mRdbStore 35 36 private constructor() { 37 } 38 /** 39 * db manager instance 40 * 41 * @return rdbStoreManager instance 42 */ 43 public static getInstance() { 44 if (globalThis.RdbStoreManagerInstance == null) { 45 globalThis.RdbStoreManagerInstance = new RdbStoreManager(); 46 } 47 return globalThis.RdbStoreManagerInstance; 48 } 49 50 public async initRdbConfig() { 51 Log.info(this.TAG + 'initRdbConfig start'); 52 const promise = dataRdb.getRdbStore(globalThis.cameraAbilityContext, 53 { 54 name: DB_NAME 55 }, DB_VERSION); 56 promise.then(async (rdbStore) => { 57 Log.info(this.TAG + 'initRdbConfig dataRdb.getRdbStore:' + rdbStore); 58 this.mRdbStore = rdbStore; 59 this.createTable(); 60 }).catch((error) => { 61 Log.error('RdbStoreManager.initRdbConfig Failed to obtain the rdbStore. Cause: ' + error.message); 62 }); 63 Log.info(this.TAG + 'initRdbConfig end'); 64 } 65 66 private async createTable() { 67 Log.info(this.TAG + 'create table start'); 68 Log.info(this.TAG + `RdbStoreConfig.CREATE_TABLE: ${CREATE_TABLE}`); 69 await this.mRdbStore.executeSql(CREATE_TABLE, []); 70 Log.info(this.TAG + 'create table end'); 71 } 72 73 public async getSettingByItem(itemName: string) { 74 Log.info(this.TAG + 'getSettingByItem start'); 75 const resultList: SettingItemInfo[] = []; 76 if (this.ifStringIsNull(itemName)) { 77 Log.warn(this.TAG + 'itemName is null'); 78 return resultList; 79 } 80 try { 81 const predicates = new dataRdb.RdbPredicates(TABLE_NAME); 82 predicates.equalTo('item_name', itemName); 83 const resultSet = await this.mRdbStore.query(predicates, []); 84 let isLast = resultSet.goToFirstRow(); 85 Log.info(this.TAG + `getSettingByItem before isLast: ${isLast}`); 86 while (isLast) { 87 const itemInfo: SettingItemInfo = new SettingItemInfo() 88 itemInfo.itemName = resultSet.getString(resultSet.getColumnIndex('item_name')); 89 itemInfo.itemValue = resultSet.getString(resultSet.getColumnIndex('item_value')); 90 resultList.push(itemInfo); 91 isLast = resultSet.goToNextRow(); 92 Log.info(this.TAG + `getSettingByItem while isLast: ${isLast}`); 93 } 94 } catch (e) { 95 Log.error(this.TAG + 'getSettingByItem error:' + e); 96 } 97 return resultList; 98 } 99 100 public async updateValue(settingItemInfo: SettingItemInfo) { 101 Log.info(this.TAG + 'updateValue start'); 102 let result = false; 103 try { 104 const predicates = new dataRdb.RdbPredicates(TABLE_NAME); 105 predicates.equalTo('item_name', settingItemInfo.itemName); 106 const updateBucket = { 107 'item_value': settingItemInfo.itemValue, 108 }; 109 Log.info(this.TAG + 'updateValue predicates: ' + JSON.stringify(predicates)) 110 Log.info(this.TAG + 'mRdbStore.update called.') 111 let changeRows = await this.mRdbStore.update(updateBucket, predicates); 112 Log.info(this.TAG + 'mRdbStore.update finished.') 113 if (changeRows == 1) { 114 Log.info(this.TAG + `updateValue updated ok: ${changeRows}`); 115 result = true; 116 } else { 117 Log.info(this.TAG + `updateValue updated not effect: ${changeRows}`); 118 const insertBucket = { 119 'item_name': settingItemInfo.itemName, 120 'item_value': settingItemInfo.itemValue, 121 }; 122 changeRows = await this.mRdbStore.insert(TABLE_NAME, insertBucket); 123 Log.info(this.TAG + `updateValue insert: ${changeRows}`); 124 result = (changeRows != -1); 125 } 126 } catch (e) { 127 Log.error(this.TAG + 'updateValue error:' + e); 128 } 129 return result; 130 } 131 132 private ifStringIsNull(str) { 133 return (str == undefined || str == '' || str == null) 134 } 135}