• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-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 rdb from '@ohos.data.relationalStore'
17import {
18  COLUMNS,
19  Contact,
20  getListFromResultSet,
21  logger,
22  SQL_CREATE_TABLE,
23  STORE_CONFIG,
24  TABLE_NAME
25} from '@ohos/common'
26
27const TAG: string = 'ContactStore'
28let rdbStore: rdb.RdbStore = undefined
29
30class ContactStore {
31  constructor() {
32  }
33
34  async getRdbStore(context: Context) {
35    try {
36      rdbStore = await rdb.getRdbStore(context, STORE_CONFIG)
37      await rdbStore.executeSql(SQL_CREATE_TABLE, [])
38      logger.info(TAG, `ContactStore executeSql done`)
39    } catch (err) {
40      logger.error(TAG, `getRdbStore error: ${JSON.stringify(err)}`)
41    }
42  }
43
44  async insert(valuesBucket: rdb.ValuesBucket) {
45    logger.info(TAG, `insert enter`)
46    if (valuesBucket === null || valuesBucket === undefined) {
47      logger.info(' insert invalid valueBuckets')
48      return
49    }
50    logger.info(TAG, ` insert  value = ${JSON.stringify(valuesBucket)}`)
51    if (rdbStore === undefined) {
52      await this.getRdbStore(getContext(this))
53    }
54    try {
55      let ret = await rdbStore.insert(TABLE_NAME, valuesBucket)
56      logger.info(TAG, ` insert finish ${ret}`)
57    } catch (err) {
58      logger.error(TAG, `insert error: ${JSON.stringify(err)}`)
59    }
60  }
61
62  async update(id: number, contact: Contact) {
63    logger.info(TAG, `update enter`)
64    if (contact === null || contact === undefined) {
65      logger.info('update invalid valueBuckets')
66      return
67    }
68    const valuesBucket = {
69      'name': contact.name,
70      'phone': contact.phone,
71      'email': contact.email,
72      'address': contact.address,
73      'remark': contact.remark
74    }
75    logger.info(TAG, `update  value = ${JSON.stringify(valuesBucket)}`)
76    if (rdbStore === undefined) {
77      await this.getRdbStore(getContext(this))
78    }
79    try {
80      let predicates = new rdb.RdbPredicates(TABLE_NAME)
81      predicates.equalTo('id', id)
82      let ret = rdbStore.update(valuesBucket, predicates)
83      logger.info(TAG, `update finish ${ret}`)
84    } catch (err) {
85      logger.error(TAG, `update error: ${JSON.stringify(err)}`)
86    }
87  }
88
89  async batchInsert(contacts: Contact[]) {
90    logger.info(TAG, `batchInsert enter`)
91    if (contacts === null || contacts === undefined) {
92      logger.info('batchInsert invalid valueBuckets')
93      return
94    }
95    let valuesBuckets = new Array()
96    contacts.forEach((item) => {
97      let valuesBucket: rdb.ValuesBucket = {
98        'name': item.name,
99        'phone': item.phone,
100        'email': item.email,
101        'address': item.address,
102        'remark': item.remark
103      }
104      valuesBuckets.push(valuesBucket)
105    })
106    logger.info(TAG, `batchInsert  value = ${JSON.stringify(valuesBuckets)}`)
107    if (rdbStore === undefined) {
108      await this.getRdbStore(getContext(this))
109    }
110    try {
111      let ret = await rdbStore.batchInsert(TABLE_NAME, valuesBuckets)
112      logger.info(TAG, `batchInsert finish ${ret}`)
113    } catch (err) {
114      logger.error(TAG, `batchInsert error: ${JSON.stringify(err)}`)
115    }
116  }
117
118  async delete(id: number) {
119    if (rdbStore === undefined) {
120      await this.getRdbStore(getContext(this))
121    }
122    try {
123      let predicates = new rdb.RdbPredicates(TABLE_NAME)
124      predicates.equalTo('id', id)
125      await rdbStore.delete(predicates)
126    } catch (err) {
127      logger.error(TAG, `delete error: ${JSON.stringify(err)}`)
128    }
129  }
130
131  async clearData() {
132    if (rdbStore === undefined) {
133      await this.getRdbStore(getContext(this))
134    }
135    try {
136      let predicates = new rdb.RdbPredicates(TABLE_NAME)
137      predicates.isNotNull('name')
138      await rdbStore.delete(predicates)
139    } catch (err) {
140      logger.error(TAG, `clearData error: ${JSON.stringify(err)}`)
141    }
142  }
143
144  async queryAll() {
145    logger.info(TAG, `query enter`)
146    if (rdbStore === undefined) {
147      await this.getRdbStore(getContext(this))
148    }
149    try {
150      let predicates = new rdb.RdbPredicates(TABLE_NAME)
151      let resultSet = await rdbStore.query(predicates, COLUMNS)
152      logger.info(TAG, `query ret: ${resultSet}`)
153      if (resultSet !== undefined) {
154        logger.info(TAG, `query resultSet.rowCount: ${JSON.stringify(resultSet.rowCount)}`)
155        return getListFromResultSet(resultSet)
156      } else {
157        return []
158      }
159    } catch (err) {
160      logger.error(TAG, `query error: ${JSON.stringify(err)}`)
161    }
162  }
163}
164
165export default new ContactStore()