1/**
2 * Copyright (c) 2022 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 */
15import HiLog from './HiLog';
16import mmsTable from '../data/tableData';
17import ohosDataRdb from '@ohos.data.rdb';
18
19const TAG = 'MmsDatabaseHelper';
20
21export default class MmsDatabaseHelper {
22    public static TABLE = {
23        SESSION: 'session'
24    };
25    private static sInstance: MmsDatabaseHelper;
26    private mRdbStore = null;
27
28
29    static getInstance() {
30        if (MmsDatabaseHelper.sInstance == null) {
31            MmsDatabaseHelper.sInstance = new MmsDatabaseHelper();
32        }
33        return MmsDatabaseHelper.sInstance;
34    }
35
36    /**
37     * Creating Database Tables
38     */
39    async createTable() {
40        if (this.mRdbStore == null) {
41            this.mRdbStore = await ohosDataRdb.getRdbStore(globalThis.mmsContext,
42                { name: mmsTable.DB.MMSSMS.config.name }, mmsTable.DB.MMSSMS.version);
43
44            await this.mRdbStore.executeSql(mmsTable.table.session, null);
45            HiLog.i(TAG, 'create session table finish');
46        } else {
47            HiLog.i(TAG, ' this.mRdbStore !== null ');
48        }
49    }
50
51    /**
52     * Query Interface
53     * @param predicates     Search criteria
54     * @return
55     */
56    async query(predicates) {
57        if (this.mRdbStore !== null) {
58            return await this.mRdbStore.query(predicates);
59        } else {
60            HiLog.w(TAG, 'mRdbStore is null, query fail.');
61            return null;
62        }
63    }
64
65    /**
66     * New data
67     */
68    async insert(tableName, valueBucket) {
69        let rowId = 0;
70        if (this.mRdbStore !== null) {
71            let insertPromise = this.mRdbStore.insert(tableName, valueBucket);
72            insertPromise.then((ret) => {
73                HiLog.i(TAG, 'insert, first done: ' + rowId);
74                rowId = ret;
75            }).catch((err) => {
76                HiLog.e(TAG, 'insert, first fail: ' + JSON.stringify(err.message));
77            })
78            await insertPromise;
79        } else {
80            HiLog.w(TAG, 'mRdbStore is null, insert fail.');
81        }
82        return rowId;
83    }
84
85    /**
86     * Update Interface
87     * @param predicates
88     * @param predicates
89     * @return
90     */
91    async update(predicates, valueBucket) {
92        if (this.mRdbStore !== null) {
93            let changedRows = await this.mRdbStore.update(valueBucket, predicates);
94            HiLog.i(TAG, 'update, changedRows=' + changedRows);
95        } else {
96            HiLog.w(TAG, 'mRdbStore is null, update fail.');
97        }
98    }
99
100    /**
101     * Deleting an Interface
102     * @param predicates
103     * @return
104     */
105    async deleteItem(predicates) {
106        if (this.mRdbStore !== null) {
107            let deletedRows = await this.mRdbStore.delete(predicates);
108            HiLog.i(TAG, 'deleteItem, deletedRows=' + deletedRows);
109        } else {
110            HiLog.w(TAG, 'mRdbStore is null, delete item fail.');
111        }
112    }
113}