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