• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * Copyright (c) 2024-2024 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 relationalStore from '@ohos.data.relationalStore';
17import Logger from '../utils/Logger';
18import DataShareConstants from '../constants/DataShareConstant';
19
20const TAG = 'RdbManager';
21const OPT_ERROR = -1;
22
23export class RdbManager {
24  private _rdbStore: relationalStore.RdbStore | undefined = undefined;
25
26  private constructor() {
27  }
28
29  private static instance: RdbManager;
30
31  /**
32   * To get single instance
33   * @returns The single instance
34   */
35  public static getInstance(): RdbManager {
36    if (!RdbManager.instance) {
37      RdbManager.instance = new RdbManager();
38    }
39    return RdbManager.instance;
40  }
41
42  async getRdbStore(context: Context): Promise<relationalStore.RdbStore | undefined> {
43    if (this._rdbStore) {
44      return this._rdbStore;
45    }
46    try {
47      this._rdbStore = await relationalStore.getRdbStore(context, DataShareConstants.STORE_CONFIG);
48      if (this._rdbStore === undefined) {
49        return this._rdbStore;
50      }
51      await this._rdbStore.executeSql(DataShareConstants.ANTO_MENU_TABLE_V2.sqlCreate);
52      Logger.info(TAG, 'getRdbStore() finished.')
53    } catch (error) {
54      Logger.info(TAG, 'getRdbStore() error.')
55    }
56    return this._rdbStore;
57  }
58
59  async insert(context: Context, table: string, values: relationalStore.ValuesBucket): Promise<number> {
60    try {
61      let rdbStore: relationalStore.RdbStore | undefined = await this.getRdbStore(context);
62      if (rdbStore === undefined) {
63        return OPT_ERROR;
64      }
65      const result: number = await rdbStore.insert(table, values);
66      return result;
67    } catch (err) {
68      Logger.error(TAG, 'insert error.')
69    }
70    return OPT_ERROR;
71  }
72
73
74  async delete(context: Context, predicates: relationalStore.RdbPredicates): Promise<number> {
75    try {
76      let rdbStore: relationalStore.RdbStore | undefined = await this.getRdbStore(context);
77      if (rdbStore === undefined) {
78        return OPT_ERROR;
79      }
80      const result: number = await rdbStore.delete(predicates);
81      return result;
82    } catch (err) {
83      Logger.error(TAG, 'delete error.')
84    }
85    return OPT_ERROR;
86  }
87
88  async query(context: Context, predicates: relationalStore.RdbPredicates,
89    columns?: Array<string>): Promise<relationalStore.ResultSet | undefined> {
90    try {
91      let rdbStore: relationalStore.RdbStore | undefined = await this.getRdbStore(context);
92      if (rdbStore === undefined) {
93        return undefined;
94      }
95      const result: relationalStore.ResultSet = await rdbStore.query(predicates, columns);
96      return result;
97    } catch (err) {
98      Logger.error(TAG, 'query error.')
99    }
100    return undefined;
101  }
102}