1/**
2 * Copyright (c) 2021 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 BaseModel from '../model/BaseModel';
16import LogUtil  from './LogUtil';
17import ohosDataRdb from '@ohos.data.rdb';
18
19const TAG = "RdbStoreUtil.js->";
20// Database name
21const STORE_CONFIG = {
22  name: "settings.db",
23}
24// Database instance object
25var rdbStore = undefined;
26/**
27 * Creating a database
28 */
29var getRdbStore = async function () {
30  if (rdbStore == null) {
31    rdbStore = await ohosDataRdb.getRdbStore(STORE_CONFIG, 1);
32  }
33  return rdbStore;
34}
35
36export default {
37  /**
38     * create data
39     */
40  createRdbStore(callback) {
41    LogUtil.info(TAG + 'get data')
42    if (!rdbStore) {
43      ohosDataRdb.getRdbStore(STORE_CONFIG, 1)
44        .then((store) => {
45          LogUtil.info(TAG + 'get data create  success' + JSON.stringify(store))
46          rdbStore = store
47          callback(true)
48      })
49        .catch((err) => {
50        LogUtil.info(TAG + 'get data createRdbStore err' + err)
51        callback(false)
52      })
53    }
54  },
55  /**
56     * create data table
57     */
58  async createTable(table) {
59    LogUtil.info(TAG + 'create table start');
60    await rdbStore.executeSql(table, null);
61    LogUtil.info(TAG + 'create table end');
62  },
63  /**
64     * insert
65     */
66  insert(tableName, rowValue) {
67    LogUtil.info('get data start: ' + tableName);
68    return rdbStore.insert(tableName, rowValue);
69  },
70  /**
71     * update
72     * @param predicates
73     * @param valueBucket
74     * @return
75     */
76  async update(predicates, rowValue) {
77    LogUtil.info(TAG + 'update start');
78    let changedRows = await rdbStore.update(rowValue, predicates);
79    LogUtil.info(TAG + 'update row count: ' + changedRows);
80  },
81  /**
82     * delete
83     * @param predicates
84     * @return
85     */
86  async deleteItem(predicates) {
87    LogUtil.info(TAG + 'get data delete item');
88    let deletedRows = await rdbStore.delete(predicates);
89    LogUtil.info(TAG + 'get data delete deletedRows' + deletedRows);
90  },
91  /**
92     * query
93     * @param tableName
94     * @return
95     */
96  getRdbPredicates(tableName) {
97    LogUtil.info(TAG + 'get data query table start')
98    let predicates = new ohosDataRdb.RdbPredicates(tableName);
99    LogUtil.info(TAG + 'get data query table end' + JSON.stringify(predicates))
100    return predicates;
101  },
102  /**
103     * get rdbData
104     * @return
105     */
106  getRdbStore() {
107    return rdbStore;
108  },
109}
110
111
112