• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 */
15
16import dataRdb from '@ohos.data.rdb'
17import { CheckEmptyUtils } from '../utils/CheckEmptyUtils'
18import { CommonConstants } from '../constants/CommonConstants'
19import { GridLayoutItemInfo } from '../bean/GridLayoutItemInfo'
20import { GridLayoutInfoColumns } from '../bean/GridLayoutInfoColumns'
21import { Logger } from '../utils/Logger'
22
23export const TABLE_NAME: string = 'launcher'
24
25export const SQL_CREATE_TABLE = 'CREATE TABLE IF NOT EXISTS launcher ' +
26'(id INTEGER PRIMARY KEY AUTOINCREMENT, ' +
27'app_name TEXT, ' +
28'appIcon_id INTEGER, ' +
29'container INTEGER, ' +
30'type_id INTEGER, ' +
31'card_id INTEGER, ' +
32'card_name TEXT, ' +
33'badge_number INTEGER, ' +
34'module_name TEXT, ' +
35'bundle_name TEXT, ' +
36'ability_name TEXT, ' +
37'area TEXT, ' +
38'page INTEGER, ' +
39'column INTEGER, ' +
40'row INTEGER)'
41
42export const STORE_CONFIG = { name: 'launcher.db' }
43const TAG: string = 'RdbModel'
44class RdbManagerModel {
45  private mRdbStore: dataRdb.RdbStore = undefined
46
47  constructor() {
48  }
49
50  /**
51   * initRdbConfig
52   *
53   * @param context
54   */
55  async initRdbConfig(context): Promise<void> {
56    Logger.info(TAG, 'initRdbConfig start')
57    this.mRdbStore = await dataRdb.getRdbStore(context, STORE_CONFIG, 1)
58    await this.mRdbStore.executeSql(SQL_CREATE_TABLE)
59    Logger.info(TAG, 'create table end')
60  }
61
62  /**
63   * deleteTable
64   *
65   * @param tableName
66   */
67  async deleteTable(tableName: string): Promise<void> {
68    Logger.info(TAG, 'deleteTable start')
69    try {
70      let detelSql = `DELETE FROM ${tableName};`
71      let detelSequenceSql = `UPDATE sqlite_sequence SET seq=0 WHERE name = '${tableName}';`
72      await this.mRdbStore.executeSql(detelSql)
73      await this.mRdbStore.executeSql(detelSequenceSql)
74      Logger.debug(TAG, 'deleteTable end')
75    } catch (e) {
76      Logger.error(TAG, `deleteTable err: ${e}`)
77    }
78  }
79
80  /**
81   * insertData
82   *
83   * @param layoutInfo
84   */
85  async insertData(layoutInfo: any) {
86    Logger.info(TAG, 'insertGridLayoutInfo start');
87    let result: boolean = true
88    if (CheckEmptyUtils.isEmpty(layoutInfo)) {
89      Logger.error(TAG, 'insertGridLayoutInfo gridlayoutinfo is empty')
90      result = false
91      return result
92    }
93    try {
94      // delete gridlayoutinfo table
95      await this.deleteTable(TABLE_NAME)
96      // insert into gridlayoutinfo
97      for (let i in layoutInfo) {
98        let layout = layoutInfo[i]
99        for (let j in layout) {
100          let element = layout[j]
101          Logger.info(TAG, `insertGridLayoutInfo i= ${i}`)
102          let item = {}
103          if (element.typeId === CommonConstants.TYPE_APP) {
104            item = {
105              'app_name': element.appName,
106              'bundle_name': element.bundleName,
107              'module_name': element.modelName,
108              'ability_name': element.abilityName,
109              'appIcon_id': element.appIconId,
110              'type_id': element.typeId,
111              'area': element.area[0] + ',' + element.area[1],
112              'page': element.page,
113              'column': element.column,
114              'row': element.row,
115              'container': -100
116            }
117            let ret = await this.mRdbStore.insert(TABLE_NAME, item)
118            Logger.debug(TAG, `insertGridLayoutInfo type is app ${i} ret: ${ret}`)
119          } else if (element.typeId === CommonConstants.TYPE_CARD) {
120            item = {
121              'app_name': element.appName,
122              'bundle_name': element.bundleName,
123              'module_name': element.modelName,
124              'ability_name': element.abilityName,
125              'card_id': element.cardId,
126              'card_name': element.cardName,
127              'type_id': element.typeId,
128              'area': element.area[0] + ',' + element.area[1],
129              'page': element.page,
130              'column': element.column,
131              'row': element.row,
132              'container': -100
133            }
134            let ret = await this.mRdbStore.insert(TABLE_NAME, item)
135            Logger.debug(TAG, `insertGridLayoutInfo type is card ${i} ret: ${ret}`)
136          }
137        }
138      }
139    } catch (e) {
140      Logger.error(TAG, `insertGridLayoutInfo error: ${e}`)
141    }
142    return result
143  }
144
145  async queryLayoutInfo() {
146    Logger.info(TAG, 'queryLayoutInfo start')
147    const resultList: GridLayoutItemInfo[] = []
148    const predicates = new dataRdb.RdbPredicates(TABLE_NAME)
149    predicates.equalTo(GridLayoutInfoColumns.CONTAINER, -100)
150      .and().orderByAsc('page').and().orderByAsc('row').and().orderByAsc('column')
151    let resultSet = await this.mRdbStore.query(predicates)
152    Logger.info(TAG, `queryLayoutInfo query,count=${resultSet.rowCount}`)
153    let isLast = resultSet.goToFirstRow()
154    while (isLast) {
155      const layoutInfo: GridLayoutItemInfo = GridLayoutItemInfo.fromResultSet(resultSet)
156      resultList.push(layoutInfo)
157      isLast = resultSet.goToNextRow()
158    }
159    resultSet.close()
160    resultSet = null
161    return resultList
162  }
163
164  async insertItem(item: GridLayoutItemInfo) {
165    if (CheckEmptyUtils.isEmpty(item)) {
166      return
167    }
168    let element = {
169      'app_name': item.appName,
170      'module_name': item.moduleName,
171      'bundle_name': item.bundleName,
172      'ability_name': item.abilityName,
173      'appIcon_id': item.appIconId,
174      'card_id': item.cardId,
175      'card_name': item.cardName,
176      'type_id': item.typeId,
177      'area': item.area[0] + ',' + item.area[1],
178      'page': item.page,
179      'column': item.column,
180      'row': item.row,
181      'container': -100
182    }
183    let ret = await this.mRdbStore.insert(TABLE_NAME, element)
184    Logger.debug(TAG, `insertGridLayoutInfo ret: ${ret}`)
185  }
186
187  async deleteItemByPosition(page: number, row: number, column: number) {
188    const predicates = new dataRdb.RdbPredicates(TABLE_NAME)
189    predicates.equalTo('page', page)
190      .and().equalTo('row', row)
191      .and().equalTo('column', column)
192    let ret = await this.mRdbStore.delete(predicates)
193    Logger.debug(TAG, `deleteItem ret: ${ret}`)
194  }
195}
196
197export let RdbManager = new RdbManagerModel()