1048147e0Sopenharmony_ci/**
2048147e0Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3048147e0Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4048147e0Sopenharmony_ci * you may not use this file except in compliance with the License.
5048147e0Sopenharmony_ci * You may obtain a copy of the License at
6048147e0Sopenharmony_ci *
7048147e0Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8048147e0Sopenharmony_ci *
9048147e0Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10048147e0Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11048147e0Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12048147e0Sopenharmony_ci * See the License for the specific language governing permissions and
13048147e0Sopenharmony_ci * limitations under the License.
14048147e0Sopenharmony_ci */
15048147e0Sopenharmony_ciimport HiLog from './HiLog';
16048147e0Sopenharmony_ciimport mmsTable from '../data/tableData';
17048147e0Sopenharmony_ciimport ohosDataRdb from '@ohos.data.rdb';
18048147e0Sopenharmony_ci
19048147e0Sopenharmony_ciconst TAG = 'MmsDatabaseHelper';
20048147e0Sopenharmony_ci
21048147e0Sopenharmony_ciexport default class MmsDatabaseHelper {
22048147e0Sopenharmony_ci    public static TABLE = {
23048147e0Sopenharmony_ci        SESSION: 'session'
24048147e0Sopenharmony_ci    };
25048147e0Sopenharmony_ci    private static sInstance: MmsDatabaseHelper;
26048147e0Sopenharmony_ci    private mRdbStore = null;
27048147e0Sopenharmony_ci
28048147e0Sopenharmony_ci
29048147e0Sopenharmony_ci    static getInstance() {
30048147e0Sopenharmony_ci        if (MmsDatabaseHelper.sInstance == null) {
31048147e0Sopenharmony_ci            MmsDatabaseHelper.sInstance = new MmsDatabaseHelper();
32048147e0Sopenharmony_ci        }
33048147e0Sopenharmony_ci        return MmsDatabaseHelper.sInstance;
34048147e0Sopenharmony_ci    }
35048147e0Sopenharmony_ci
36048147e0Sopenharmony_ci    /**
37048147e0Sopenharmony_ci     * Creating Database Tables
38048147e0Sopenharmony_ci     */
39048147e0Sopenharmony_ci    async createTable() {
40048147e0Sopenharmony_ci        if (this.mRdbStore == null) {
41048147e0Sopenharmony_ci            this.mRdbStore = await ohosDataRdb.getRdbStore(globalThis.mmsContext,
42048147e0Sopenharmony_ci                { name: mmsTable.DB.MMSSMS.config.name }, mmsTable.DB.MMSSMS.version);
43048147e0Sopenharmony_ci
44048147e0Sopenharmony_ci            await this.mRdbStore.executeSql(mmsTable.table.session, null);
45048147e0Sopenharmony_ci            HiLog.i(TAG, 'create session table finish');
46048147e0Sopenharmony_ci        } else {
47048147e0Sopenharmony_ci            HiLog.i(TAG, ' this.mRdbStore !== null ');
48048147e0Sopenharmony_ci        }
49048147e0Sopenharmony_ci    }
50048147e0Sopenharmony_ci
51048147e0Sopenharmony_ci    /**
52048147e0Sopenharmony_ci     * Query Interface
53048147e0Sopenharmony_ci     * @param predicates     Search criteria
54048147e0Sopenharmony_ci     * @return
55048147e0Sopenharmony_ci     */
56048147e0Sopenharmony_ci    async query(predicates) {
57048147e0Sopenharmony_ci        if (this.mRdbStore !== null) {
58048147e0Sopenharmony_ci            return await this.mRdbStore.query(predicates);
59048147e0Sopenharmony_ci        } else {
60048147e0Sopenharmony_ci            HiLog.w(TAG, 'mRdbStore is null, query fail.');
61048147e0Sopenharmony_ci            return null;
62048147e0Sopenharmony_ci        }
63048147e0Sopenharmony_ci    }
64048147e0Sopenharmony_ci
65048147e0Sopenharmony_ci    /**
66048147e0Sopenharmony_ci     * New data
67048147e0Sopenharmony_ci     */
68048147e0Sopenharmony_ci    async insert(tableName, valueBucket) {
69048147e0Sopenharmony_ci        let rowId = 0;
70048147e0Sopenharmony_ci        if (this.mRdbStore !== null) {
71048147e0Sopenharmony_ci            let insertPromise = this.mRdbStore.insert(tableName, valueBucket);
72048147e0Sopenharmony_ci            insertPromise.then((ret) => {
73048147e0Sopenharmony_ci                HiLog.i(TAG, 'insert, first done: ' + rowId);
74048147e0Sopenharmony_ci                rowId = ret;
75048147e0Sopenharmony_ci            }).catch((err) => {
76048147e0Sopenharmony_ci                HiLog.e(TAG, 'insert, first fail: ' + JSON.stringify(err.message));
77048147e0Sopenharmony_ci            })
78048147e0Sopenharmony_ci            await insertPromise;
79048147e0Sopenharmony_ci        } else {
80048147e0Sopenharmony_ci            HiLog.w(TAG, 'mRdbStore is null, insert fail.');
81048147e0Sopenharmony_ci        }
82048147e0Sopenharmony_ci        return rowId;
83048147e0Sopenharmony_ci    }
84048147e0Sopenharmony_ci
85048147e0Sopenharmony_ci    /**
86048147e0Sopenharmony_ci     * Update Interface
87048147e0Sopenharmony_ci     * @param predicates
88048147e0Sopenharmony_ci     * @param predicates
89048147e0Sopenharmony_ci     * @return
90048147e0Sopenharmony_ci     */
91048147e0Sopenharmony_ci    async update(predicates, valueBucket) {
92048147e0Sopenharmony_ci        if (this.mRdbStore !== null) {
93048147e0Sopenharmony_ci            let changedRows = await this.mRdbStore.update(valueBucket, predicates);
94048147e0Sopenharmony_ci            HiLog.i(TAG, 'update, changedRows=' + changedRows);
95048147e0Sopenharmony_ci        } else {
96048147e0Sopenharmony_ci            HiLog.w(TAG, 'mRdbStore is null, update fail.');
97048147e0Sopenharmony_ci        }
98048147e0Sopenharmony_ci    }
99048147e0Sopenharmony_ci
100048147e0Sopenharmony_ci    /**
101048147e0Sopenharmony_ci     * Deleting an Interface
102048147e0Sopenharmony_ci     * @param predicates
103048147e0Sopenharmony_ci     * @return
104048147e0Sopenharmony_ci     */
105048147e0Sopenharmony_ci    async deleteItem(predicates) {
106048147e0Sopenharmony_ci        if (this.mRdbStore !== null) {
107048147e0Sopenharmony_ci            let deletedRows = await this.mRdbStore.delete(predicates);
108048147e0Sopenharmony_ci            HiLog.i(TAG, 'deleteItem, deletedRows=' + deletedRows);
109048147e0Sopenharmony_ci        } else {
110048147e0Sopenharmony_ci            HiLog.w(TAG, 'mRdbStore is null, delete item fail.');
111048147e0Sopenharmony_ci        }
112048147e0Sopenharmony_ci    }
113048147e0Sopenharmony_ci}