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